Jump to content

Matches multiple words in regex?


Andrew R

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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  :P

 

Thanks a million anyway

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.