hackerspk Posted August 13, 2011 Share Posted August 13, 2011 i have a string e.g $str = "This is my [Test] string"; i want to make it like this <a href="/index.php?word=Test">Test</a> means every word inside [] convert to click able link Thanks Link to comment https://forums.phpfreaks.com/topic/244677-replace-word/ Share on other sites More sharing options...
floridaflatlander Posted August 13, 2011 Share Posted August 13, 2011 Do you mean replace Test with a hyperlink Test? $str = "This is my [Test] string"; Link to comment https://forums.phpfreaks.com/topic/244677-replace-word/#findComment-1256760 Share on other sites More sharing options...
TOA Posted August 13, 2011 Share Posted August 13, 2011 http://php.net/manual/en/function.str-replace.php This should help Link to comment https://forums.phpfreaks.com/topic/244677-replace-word/#findComment-1256762 Share on other sites More sharing options...
hackerspk Posted August 13, 2011 Author Share Posted August 13, 2011 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 More sharing options...
PFMaBiSmAd Posted August 13, 2011 Share Posted August 13, 2011 You would use preg_replace - <?php $str = "this is [my] test [string] and [string] is very long [with] so many [words]"; $str = preg_replace("/\[(.*?)\]/is","<a href='/index.php?word=$1'>$1</a>",$str); echo $str; ?> Link to comment https://forums.phpfreaks.com/topic/244677-replace-word/#findComment-1256831 Share on other sites More sharing options...
PFMaBiSmAd Posted August 13, 2011 Share Posted August 13, 2011 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.