Log in

View Full Version : Small trouble with regular expressions



young_coder
02-25-2010, 01:42 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:

<?php
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
Cheers!

james438
02-25-2010, 06:55 PM
Hi,

I didn't look too closely at all of your code, but try replacing

$text = preg_replace('/(<a.+>)(\.+)(<\/a>)/Ue',"'$1'.preg_replace('{.+/}','','$2').'$3'",$text);
with

$text = preg_replace('/(<a.*?>).*\/(.*?<\/a>)/','$1$2',$text);

james438
02-26-2010, 07:37 AM
I am not sure how to ask this, but out of curiosity what is your code supposed to do? How is it used by the user?

young_coder
02-26-2010, 09:25 PM
One nice man on other forum helped me.. this works...

$output = preg_replace('%(<a\b[^>]+>)(.*?/)([^/<]+</a>)%', '$1$3', $input);

And this is something that I made for my wordpress blog… for interlinking and similar things… I know that there are some plugins which doing same thing, but I don’t like the way they working…