Click to See Complete Forum and Search --> : order the result of preg_match_all


Tabo
09-05-2008, 04:06 PM
When using preg_match_all, how can I order the array so that items are in the same order as they appeared in the test string, for instance:

"one two one two" ran through preg_match_all to change one to abc, the resulting array is:

abc, abc, two, two.

How would I make it so that it keeps its order, so the array would be abc, two, abc, two,

NogDog
09-05-2008, 07:44 PM
preg_match_all('/\b(one|two)\b/', $text, $matches);

I believe in this case you would find $matches[1] would be an array of each value in the order it was found, but I haven't tested it.

andre4s_y
09-06-2008, 01:31 AM
AFAIK,
You can not replace "one" to "abc" using preg_match_all() function.. You need additional function such as preg_replace() function.

preg_match_all() function only match certain pattern on certain subject, and capture them, if you want to..

Tabo
09-06-2008, 04:54 AM
AFAIK,
You can not replace "one" to "abc" using preg_match_all() function.. You need additional function such as preg_replace() function.

preg_match_all() function only match certain pattern on certain subject, and capture them, if you want to..

Oh sorry, I forgot to explain.
I am using preg_match_all() to create an array of elements which match the search, then using a loop to replace those elements with str_replace.