View Full Version : Small trouble with regular expressions

02-25-2010, 02:39 PM
Hey guys!
Please will somebody help me with this?
Link now looks like this:

<a href="this-is-some-folder/word1-word2-word3.html" target="_blank">this is some folder/word1 word2 word3</a>

and I need to be without ’this is some folder/’ (highlighted with red) like this

<a href="this-is-some-folder/word1-word2-word3.html" target="_blank">word1 word2 word3</a>

This is my PHP code:

function makeLinksFromWords($text)
$text = html_entity_decode($text);
$text = " ".$text;

$text = preg_replace('/ link_in_folder:(\S+)/e', '" <a href=\""."\\1.html\""." target=\"_blank\">".str_replace("-"," ","\\1")."</a>"', $text);
$text = preg_replace('/(<a.+>)(\.+)(<\/a>)/Ue',"'$1'.preg_replace('{.+/}','','$2').'$3'",$text);
return $text;

// Live Example
echo makeLinksFromWords("This is a example for link in folder: link_in_folder:this-is-some-folder/word1-word2-word3");

I would appreciate little help with this one.
Thank you advance

02-25-2010, 04:03 PM
$output = preg_replace('%(<a\b[^>]+>)(.*?/)([^/<]+</a>)%', '$1$3', $input);

02-25-2010, 05:32 PM
What’s the beautiful piece of code...
Thank you very much... :)