Jump to content

Archived

This topic is now archived and is closed to further replies.

Invincible

Arrays in preg_replace

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

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.