问题
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.
回答1:
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 multbyte 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?
回答2:
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.
来源:https://stackoverflow.com/questions/7675627/multi-byte-function-to-replace-preg-match-all