Full-Demon Posted October 18, 2007 Share Posted October 18, 2007 Hi, When I have a subject and search for a certain character in it (where the character before and after that character has certain conditions) it doesn't gives me the right output. preg_match_all('/(\W|\A)(\=)(\W|\Z)/', 'blahblah == lala', $matches); Result: Array ( [0] => Array ( [0] => == [1] => = ) [some more sub arrays here] ) This code checks for the character '=' and gets the surrounding characters too. The first character is matched alright (' =='), but the second one not (' = ') (note the spaces). The second result should be ('== '). Why does the second result ignores the first '='? And how to fix this? Thanks in advance, FD Link to comment https://forums.phpfreaks.com/topic/73808-preg_match_all-doesnt-work-properly/ Share on other sites More sharing options...
effigy Posted October 18, 2007 Share Posted October 18, 2007 Once something is matched the engine continues to move forward. You can utilize offsets and \G to tell it where to start. I'm not sure how efficient this is, but to my knowledge it's the only way to pull it off in PHP: <pre> <?php $str = 'blahblah == lala'; $offset = 0; $str_len = strlen($str); while ($offset < $str_len) { if (preg_match('/\G(\W|\A)(=)(\W|\Z)/', $str, $matches, NULL, $offset)) { print_r($matches); } ++$offset; } ?> </pre> Link to comment https://forums.phpfreaks.com/topic/73808-preg_match_all-doesnt-work-properly/#findComment-372375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.