Reputation: 1264
So far I managed to isolate and count pictures in a string by doing this:
preg_match_all('#<img([^<]+)>#', $string, $temp_img);
$count=count($temp_img[1]);
I would like to do something similar with parts that would look like this:
"code=mYrAnd0mc0dE123".
For instance, let's say I have this string:
$string="my first code is code=aZeRtY and my second one is code=qSdF1E"
I would like to store "aZeRtY" and "qSdF1E" in an array.
I tried a bunch of regex to isolate the "code=..." but none has worked for me. Obviously, regex is beyond me.
Upvotes: 1
Views: 967
Reputation: 6946
This:
$string = '
code=jhb2345jhbv2345ljhb2435
code=jhb2345jhbv2345ljhb2435
code=jhb2345jhbv2345ljhb2435
code=jhb2345jhbv2345ljhb2435
';
preg_match_all('/(?<=code=)[a-zA-Z0-9]+/', $string, $matches);
echo('<pre>');
print_r($matches);
echo('</pre>');
Outputs:
Array
(
[0] => Array
(
[0] => jhb2345jhbv2345ljhb2435
[1] => jhb2345jhbv2345ljhb2435
[2] => jhb2345jhbv2345ljhb2435
[3] => jhb2345jhbv2345ljhb2435
)
)
However without a suffixing delimiter, it won't work correctly if this pattern is concatenated, eg: code=jhb2345jhbv2345ljhb2435code=jhb2345jhbv2345ljhb2435
But perhaps that won't be a problem for you.
Upvotes: 1
Reputation: 2069
Are you looking for this?
preg_match_all('#code=([A-Za-z0-9]+)#', $string, $results);
$count = count($results[1]);
Upvotes: 4