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 Quote Link to comment 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"; Quote Link to comment 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 Quote Link to comment 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> Quote Link to comment 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; ?> Quote Link to comment 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; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.