Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multi-byte function to replace preg_match_all?

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.

like image 827
user151841 Avatar asked Oct 06 '11 14:10

user151841


People also ask

What is the difference between Preg_match and Preg_match_all?

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.

What is the use of Preg_match in PHP?

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.


2 Answers

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?

like image 84
hakre Avatar answered Sep 22 '22 09:09

hakre


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.

like image 37
MarcoP Avatar answered Sep 19 '22 09:09

MarcoP