Jump to content

replace word


hackerspk

Recommended Posts

i have a long string like

$str = "this is [my] test [string] and [string] is very long [with] so many pwords]"

 

i know str_replace fuction but when i replace i got this result

$str = str_replace( "[", "<a href=\"http://localhost/salar/story.php?urdu=,$str;

$str = str_replace( ]", "\"></a>",$str;

<a href="/index.php?word=test"></a>

but i want this result for each word in []

<a href="/index.php?word=test">test</a>

Link to comment
https://forums.phpfreaks.com/topic/244677-replace-word/#findComment-1256770
Share on other sites

On the outside chance that you actually just have a list of words (that were searched for) that you want to changed into links if they are found in an article or story -

<?php
$str = "this is my test string and string is very long with so many words";
$search = "my|string|with|words";
$str = preg_replace("/($search)/is","<a href='/index.php?word=$1'>$1</a>",$str);
echo $str;
?>

Link to comment
https://forums.phpfreaks.com/topic/244677-replace-word/#findComment-1256855
Share on other sites

Archived

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

×
×
  • 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.