Andrew R Posted May 24, 2011 Share Posted May 24, 2011 Hi there I am beginning to learn regex in php. I was wondering why the following regex pattern wasn't finding the two words in the string and replacing them with welcome? '/(Hello).*?(Hi)/is' $patterns = array(); $patterns[0] = '/(Hello).*?(Hi)/is'; $replacements = array(); $replacements[0] = 'Welcome'; ksort($patterns); ksort($replacements); echo preg_replace($patterns, $replacements, $string); Any help would be much appreciated. Thanks a million! Quote Link to comment https://forums.phpfreaks.com/topic/237315-matches-multiple-words-in-regex/ Share on other sites More sharing options...
Adam Posted May 24, 2011 Share Posted May 24, 2011 What are you expecting the output to be? Your replacement for the entire regex match is "Welcome" - the words won't be replaced individually and the text in-between will not be preserved. If you are just replacing words case-insensitively, then you should use str_ireplace. If not, give us some examples of the input and output strings you expect. Quote Link to comment https://forums.phpfreaks.com/topic/237315-matches-multiple-words-in-regex/#findComment-1219517 Share on other sites More sharing options...
Andrew R Posted May 24, 2011 Author Share Posted May 24, 2011 Thanks for your reply. For example if someone enters in Hello. Hi. It will be changed to Welcome. Quote Link to comment https://forums.phpfreaks.com/topic/237315-matches-multiple-words-in-regex/#findComment-1219523 Share on other sites More sharing options...
Adam Posted May 24, 2011 Share Posted May 24, 2011 Well with your code, and $string set to "Hello. Hi", that's what happens? Where do you define $string? Quote Link to comment https://forums.phpfreaks.com/topic/237315-matches-multiple-words-in-regex/#findComment-1219524 Share on other sites More sharing options...
Andrew R Posted May 24, 2011 Author Share Posted May 24, 2011 Thanks for the reply! I should have said $string is a post eg = $string = $_POST['text']; So for for example a user may enter Hi. They may enter Hello. I want both to change to Welcome basically. Quote Link to comment https://forums.phpfreaks.com/topic/237315-matches-multiple-words-in-regex/#findComment-1219528 Share on other sites More sharing options...
Adam Posted May 24, 2011 Share Posted May 24, 2011 Ah.. Then you shouldn't be using a regular expression like that. /(Hello).*?(Hi)/is This would only replace a string that contains both "Hello" and "Hi", in that order, and anything in the middle of the two would be lost. What you would do in that situation is: /(hello|hi)/is That would match "hello" or "hi" (case-insensitively since you have the 'i' flag') and replace them individually. However a regular expression isn't required for such a replacement, you can just use str_ireplace: $matches = array('hello', 'hi'); $replacement = 'Welcome'; echo str_ireplace($matches, $replacement, $string); Quote Link to comment https://forums.phpfreaks.com/topic/237315-matches-multiple-words-in-regex/#findComment-1219531 Share on other sites More sharing options...
Andrew R Posted May 24, 2011 Author Share Posted May 24, 2011 Thanks a million for that! I guess I could just use str_ireplace but I might have hundreds/thousands of potential words to replace in a string. Which is the most efficient for doing this? Also I might be changing the order of certain words in the string and stuff. As stupid as this sounds I'm building a gangster talk generator for a uni project Thanks a million anyway Quote Link to comment https://forums.phpfreaks.com/topic/237315-matches-multiple-words-in-regex/#findComment-1219534 Share on other sites More sharing options...
Adam Posted May 24, 2011 Share Posted May 24, 2011 Anything that's just basic string replacement, use str_ireplace() -- or str_replace() if you can, as it's faster. If there's multiple replacements of course you'll need to vary things slightly, so that you can specify the match and the replacement for each. Best way would be to define an array, where the keys are the matches and the values are the replacements: $replacements = array( // Key => Value 'Hello' => 'Welcome', 'Hi' => 'Welcome', ); Then you can use the array_keys and array_values functions to pass in the correct values to the replace function: str_ireplace(array_keys($replacements), array_values($replacements), $string); That would be easier than trying to maintain two arrays. Anything that's beyond the complexity of simple word replacement though would possibly need to be done using a regex, for example switching the order of two words next to each other: preg_replace('/(word1) (word2)/i', '$2 $1', $string); However don't forget about the host of other string functions available, as they're generally always faster than regex alternatives. Quote Link to comment https://forums.phpfreaks.com/topic/237315-matches-multiple-words-in-regex/#findComment-1219539 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.