1
$mystring = "@blablabla Kayit Ol ogrencino:1176160"

This is my string, placament of ogrencino:1176160 in string can change, but others will be stable.

Like:

$mystring = "@blablabla ogrencino:1176160 Kayit Ol" etc.

How can i parse "1176160"?

jh314
27.9k16 gold badges67 silver badges83 bronze badges
asked Jun 26, 2013 at 17:11
2
  • Are they the only numbers? $number = preg_replace("/\D/","",$input); Commented Jun 26, 2013 at 17:13
  • they are but, it can be "@blablabla 15 Kayit Ol ogrencino:1176160" too. Sorry i didnt clarify myself... I must take after ogrencino: but only numbers Commented Jun 26, 2013 at 17:14

4 Answers 4

2

Use preg_match like so:

$mystring = "@blablabla ogrencino:1176160 Kayit Ol";
// Changed regular expression to match Kolink's comment
preg_match('/(?<=\bogrencino:)\d+/', $mystring, $matches);
print_r($matches);
// displays Array ( [0] => 1176160 )

If it appears more than once in the string you can do preg_match_all()

answered Jun 26, 2013 at 17:13
Sign up to request clarification or add additional context in comments.

5 Comments

+1 It would be better if you also add some explanation to it :)
Since only the numbers are needed, /(?<=\bogrencino:)\d+/ would be a better regex.
@Kolink Thank you for the better expression. I'm kind of a novice when it comes to regex.
What if they wont only numbers ? Can i still use it ?
No you can't use this regular expression if you want to get non-numeric values. If you want to get something other than numbers you need to find a new terminator. So if for example the string will always have a space after it then we can use that
1

You can also look at this regex:

(?<=ogrencino:)(.+?)[\s$]

It is independent of what value exists after ogrencino:. (It can be digit or non-digit)

Regex break up:

(?<=ogrencino:) = Positive look behind, checks `ogrencino:` exists behind the next criteria.
.+? = Any thing (except new line) one or more time, but lazy due to `?`.
[\s$] = after match either whitespace or end of line will exists.
answered Jun 26, 2013 at 18:43

Comments

0

You want to look into regular expressions, particularly preg_match.

For your specific example do something like this:

$mystring = "@blablabla ogrencino:1176160 Kayit Ol";
$matches = array();
preg_match( '/ogrencino\d+/', $mystring, $matches);
echo $matches[0]; // ogrencino:1176100

What this does is find every instance of "one or more digits" (that's what the \d+ is) and read every match into $matches

answered Jun 26, 2013 at 17:17

Comments

0

Without REGEX:

$string = "@blablabla ogrencino:1176160 Kayit Ol";
$beg = strpos($string,"ogrencino:") + strlen("ogrencino:");
$end = strpos($string," ",$beg);
$mynumber = substr($string,$beg,$end-$beg); //1176100
answered Jun 26, 2013 at 17:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.