Jump to content

Arrays in preg_replace


Invincible

Recommended Posts

Hello, I am using this simple preg_replace function to turn "Google" into a linkt o the Google site (not really what I am using it for I am just using it as an example)

[code]
$txt = preg_replace( "#Google#is", "<a href='http://www.google.com/'>Google</a>", $txt );
[/code]

But what about if I want to link Yahoo, well the simple option would be to just repeat the line. However, what if I start adding in MSN, Alta Vista, it will all get very confusing, so I create an array.

The array setting the words:
[code]
array(
"Google",
"Yahoo",
"MSN"
);
[/code]

And the array with the links:

[code]
array(
"<a href='http://www.google.com/'>Google</a>",
"<a href='http://www.yahoo.com/'>Yahoo</a>",
"<a href='http://www.msn.com/'>MSN</a>"
);[/code]

Alternativly, we could have this:

[code]
array(
"Google"  =>  "<a href='http://www.google.com/'>Google</a>",
"Yahoo"  =>  "<a href='http://www.yahoo.com/'>Yahoo</a>",
"MSN"  =>  "<a href='http://www.msn.com/'>MSN</a>"
);
[/code]


But how do I now get this to work!? My itnitial thought was this:

[code]<?php

$names = array(
"Google",
"Yahoo",
"MSN"
);

$links = array(
"<a href='http://www.google.com/'>Google</a>",
"<a href='http://www.yahoo.com/'>Yahoo</a>",
"<a href='http://www.msn.com/'>MSN</a>"
);

$txt = preg_replace( "#".$names."#is", $links, $txt );

?>[/code]

However, this of course, did not worsk, and I was produced with this error:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
Warning: preg_replace(): Parameter mismatch, pattern is a string while replacement in an array. in /home/****/public_html/forums/keywords.php on line 15[/quote]

So my question is, how do I enable arrays in preg_replace!?



Thanks in adavcne for your help,
Mark
Link to comment
Share on other sites

put the pattern in an array aswell, study the [a href=\"http://no.php.net/manual/en/function.preg-replace.php\" target=\"_blank\"]Manual: [a href=\\\"http://no.php.net/manual/en/function.preg-replace.php\\\" target=\\\"_blank\\\"]http://no.php.net/manual/en/function.preg-replace.php[/a][/a]
Link to comment
Share on other sites

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]put the pattern in an array[/quote]
How?

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Manual: [a href=\"http://no.php.net/manual/en/function.preg-replace.php\" target=\"_blank\"]http://no.php.net/manual/en/function.preg-replace.php[/a][/quote]
Broken link, however I have had a look at the example for preg_replace and they are using an array which is exactly what I am doing and their's works fine.

Please could you just post what I would need to do for the code I ahve laid out (it is a very simple code)and I will suss how this works.

Thanks
Link to comment
Share on other sites

You're not putting the pattern in an array, only pieces of it - this works however:
[code]
<?php

$names = array(
"#Google#is",
"#Yahoo#is",
"#MSN#is"
);

$links = array(
"<a href='http://www.google.com/'>Google</a>",
"<a href='http://www.yahoo.com/'>Yahoo</a>",
"<a href='http://www.msn.com/'>MSN</a>"
);

$txt = "bacsk ksaj Google jha djshfu MSN dhfow Yahoo";

$txt = preg_replace( $names, $links, $txt );

echo $txt;

?>
[/code]
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.