Web Hosting Talk







View Full Version : Any PHP guru? mastering -> preg_replace


coume
04-07-2005, 05:11 PM
Hello,

I have been playing with that for 2 days and I can't manage to get that working :(

Basically, I would like to strip my variable from the <a href > links...

ie: My variable $tmp=<a href="http://www.mysite.com">blablablablba</a>
and I would like to get the variable to look like that: $tmp=blablablablba

This should be possible using preg_replace but I can't find how :(

So if there is a guru mastering php, I hope to hear from you ;)
Ludo

insanelymacintosh
04-07-2005, 07:10 PM
At first look I said to myself... on this will be easy. After looking at it again and trying to do it myself I see that it wasn't so easy. But Iwas able to get you setup... I think. Use the following and let me know if this works for you.


<?
$tmp = '<a href="http://www.mysite.com">blablablablba</a>';

$trimfirst = '</a>';
$trimsecond = "<a href=";

// this will take off the </a> and the <a href=
$tmpfirst = str_replace($trimfirst, " ", $tmp);
$tmpsecond = str_replace($trimsecond, " ", $tmpfirst);

// this will split the tmpsecond to show you the URL that it is going
// to and the name of the link
$split = explode(">", $tmpsecond);

// this will print it all out for you
echo 'This is the URL it is going to: ' . $split[0] . '<br />';
echo 'This is the text in the URL: ' . $split[1];
?>

X-TechMedia
04-07-2005, 08:32 PM
Somthing like this might help


<?
$tmp = '<a href="http://www.mysite.com">blablablablba</a>';

preg_match('/(<a href=")(.*)(">)(.*)(<\/a>+)/i',$tmp, $matches);

$tmp = $matches[4];
?>

coume
04-08-2005, 03:20 AM
thank you both for the answers!

I managed to get written the links like:
link_http - link_text

Now I will try to play a bit more to only trim the URLs that links outside my domain.

Cheers,
Ludo

runesolutions
04-08-2005, 06:39 AM
Not sure if this helps at all, but if you just want the bit outside of the HTML tags, you could use strip_tags.

$tmp = strip_tags('<a href="http://www.mysite.com">blablablablba</a>')

would yield: blablablablba

mfonda
04-08-2005, 03:03 PM
bleh, when will people learn, regex is seldomly the answer.

as disoft stated, just use strip_tags()
http://us3.php.net/strip_tags

nethnet
04-10-2005, 05:55 PM
@ MFonda,

REGEX/PCRE is almost always the answer when trying to strip/replace/manipulate strings. strip_tags() is a neat little function, but the original poster never mentioned that they want to strip the string of all of the HTML tags. It looks to me that he only wants to get rid of A tags that link to somewhere out of his site.

@ coume,

I threw this function together quickly, so it might not work. A quick glance through it, though, and I can't see anything wrong with it. Let me know.

// $domain must be formatted with each forwardslash escaped (this should be the 'safe' domain for links)

function a_strip($html, $domain){
$pcre = "/(<a href=\")([^" . $domain . ")(\".*>)(.*)(<\/a>)/si"
$html = preg_replace($pcre, $4, $html);
return $html;
}

-Derrick

UberTec
04-10-2005, 06:07 PM
how about a simple regex like this if you are not going to use strip_tags

$tmp = '<a href="http://www.mysite.com">blablablablba</a>';
$tmp = preg_replace("/<a(.*?)>(.*?)</a>/i", "//2", $tmp);

(untested but should work)

nethnet
04-10-2005, 07:56 PM
In case it wasn't blatantly obvious why the most recent code would produce errors, let me clarify the problem. The forwardslash is being used as your delimiters, and therefore not escaping the one in the closing A tag makes the PCRE believe that a>... serves as a regular expression modifier, and it obviously isn't. Either escape the forwardslash or change the delimiters to something unique.

-Derrick

UberTec
04-11-2005, 10:05 AM
Originally posted by nethnet
In case it wasn't blatantly obvious why the most recent code would produce errors, let me clarify the problem. The forwardslash is being used as your delimiters, and therefore not escaping the one in the closing A tag makes the PCRE believe that a>... serves as a regular expression modifier, and it obviously isn't. Either escape the forwardslash or change the delimiters to something unique.

-Derrick

Ah woops, thanks for pointing that out :)

nethnet
04-11-2005, 10:29 AM
Not a problem, flush().

-Derrick