Jump to content


Photo

Arrays in preg_replace


  • Please log in to reply
3 replies to this topic

#1 Invincible

Invincible
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 19 May 2006 - 04:25 PM

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)

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

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:
array(
"Google",
"Yahoo",
"MSN"
);

And the array with the 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>"
);

Alternativly, we could have this:

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


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

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

?>

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

#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 19 May 2006 - 04:31 PM

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]

#3 Invincible

Invincible
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 19 May 2006 - 04:51 PM

[!--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

#4 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 19 May 2006 - 06:23 PM

You're not putting the pattern in an array, only pieces of it - this works however:
<?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;

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users