Reputation: 113
$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"
?
Upvotes: 1
Views: 90
Reputation: 11233
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.
Upvotes: 1
Reputation: 12031
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()
Upvotes: 2
Reputation: 397
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
Upvotes: 0
Reputation: 542
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
Upvotes: 0