Log in

View Full Version : Small trouble with preg_replace



young_coder
02-21-2010, 01:21 PM
Hey guys!
I'm working on this little script but I’m novice with PHP and I can not find out solution for this alone, so please will somebody help me.
Link looks like this:
<a href="word1-word2-word3">word1-word2-word3</a>
And I need to be without hyphens like this in part highlighted with red
<a href="word1-word2-word3">word1 word2 word3</a>

Here is my script

<?
function makeLinksFromWords($text)
{
$text = html_entity_decode($text);
$text = " ".$text;

$text = preg_replace('/ link:(\S+)/', ' <a href="$1">$1</a>', $text);
return $text;
}

// Live Example
echo makeLinksFromWords("This is a example for link link:word1-word2-word3");
?>


Would appreciate some help with this one and thank you advance
Cheers!

young_coder
02-21-2010, 02:48 PM
This is Ok :)

$text = preg_replace('/ link:(\S+)/e', '" <a href="."\\1".">".str_replace("-"," ","\\1")."</a>"', $text);

auriaks
02-21-2010, 03:23 PM
try this... :)


$text = str_replace('-', ' ', $text);

james438
02-22-2010, 03:48 AM
$text=preg_replace('/(<a.+>)(.+)(<\/a>)/Ue',"'$1'.str_replace('-',' ','$2').'$3'",$text);

Just remember some of the rules dealing with the e modifier http://us.php.net/manual/en/reference.pcre.pattern.modifiers.php

That is a good thought auriaks. My first thought was to try and use str_replace, but after the text for the link was fixed the link would no longer work.