I'm looking for a multi-byte function to replace preg_match_all()
. I need one that will give me an array of matched strings, like the $matches
argument from preg_match()
. The function mb_ereg_match()
doesn't seem to do it -- it only gives me a boolean indicating if there were any matches.
Looking at the mb_* functions page, I don't offhand see anythng that replaces the functionality of preg_match()
. What do I use?
Edit I'm an idiot. I originally posted this question asking for a replacement for preg_match
, which of course is ereg_match
. However both those only return the first result. What I wanted was a replacement for preg_match_all
, which returns all match texts. But anyways, the u
modifier works in my case for preg_match_all
, as hakre pointed out.
preg_match stops looking after the first match. preg_match_all , on the other hand, continues to look until it finishes processing the entire string. Once match is found, it uses the remainder of the string to try and apply another match.
PHP | preg_match() Function. This function searches string for pattern, returns true if pattern exists, otherwise returns false. Usually search starts from beginning of subject string. The optional parameter offset is used to specify the position from where to start the search.
Have you taken a look into mb_ereg
?
Additionally, you can pass an UTF-8 encoded string into preg_match
using the u
modifier, which might be the kind of multi-byte support you need. The other option is to encode into UTF-8 and then encode the results back.
See as well an answer to a related question: Are the PHP preg_functions multibyte safe?
PHP: preg_grep manual
$matches = preg_grep('/(needles|to|find)/u', $inputArray);
Returns an array indexed using the keys from the input array.
Note the /u modifier which enables multibyte support.
Hope it helps others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With