Server Help

Trash Talk - PHP regex

2dragons - Sat Apr 02, 2005 10:25 pm
Post subject: PHP regex
I finally had to ask here because I cannot get this to work.

I have something like such:

<?php

$text = '<a href="http://www.somelink.com">';

$text = preg_replace( "<a href=\"(.*?)\">", "\1", $text );

echo $text;
?>

And I'm hoping to get the actual link ie:

http://www.somelink.com

I've never touched regex before, and I've tried numerous patterns:

'/\<a href=\"(.*?)\"\>/'
'/\<a href="(.*?)"\>/'
'\<a href=\"(.*?)\"\>'
'/<a href=\"(.*?)\">/'

But I cannot get anything to work, ideas?
Mine GO BOOM - Sat Apr 02, 2005 10:35 pm
Post subject:
Regex Coach is a very good tool for learning and practicing regex expressions. As for preg_replace, the manual tells you a good chunk about that.

As to your problem, this worked for me:
Code: Show/Hide
<?php

$text = '<a href="http://www.somelink.com">';

$text = preg_replace( "/<a href=\"(.*?)\">/i", "$1", $text );

echo $text;
?>

2dragons - Sat Apr 02, 2005 10:54 pm
Post subject:
Thanks for the reply, I've been use Regex Coach for awhile, it definently helps.

But did you actually try that within php?

It does not work for me, and I had attempted that before.
Mine GO BOOM - Sun Apr 03, 2005 12:04 am
Post subject:
Worked fine for me when tried it out. The server I tried it on is running PHP/4.3.10.
Cerium - Sun Apr 03, 2005 12:25 am
Post subject:
If all youre trying to do is remove the anchor and replace it with the actual URL, this will work fine:

Code: Show/Hide

<?php
   $strHTML = "<HTML><HEAD><TITLE>My site pwns you</TITLE></HEAD> <BODY>This is a link to my site, omg you should click it! <A Target=\"_top\" Href=\"http://www.x2productions.net\" OnMouseOver=\"javascript:SomeFunction()\">MY SITE! CLICK HERE! OMG!</A></BODY></HTML>";
   $strHTML = preg_replace("|<A [^>]*HREF=\"(.*?)\"[^>]*>.*?</A>|i", "$1", $strHTML);

   print $strHTML;
?>


Also, the regex allows for any number of properties for the anchor instead of just 'A HREF'.

Tested with php 5, with no problems.


Also, it looks like your backreference was wrong... I believe preg_replace wants a double backslash when using that notation:

Quote:

Replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one.

2dragons - Sun Apr 03, 2005 1:05 am
Post subject:
Well I did give yours a shot Cerium and it does work, however I now believe my problem lies within the actual bit of text I'm trying to use it on.

<font title="verdana"><a href="http://www.host.com/abc3.cfm?abc3id=180&amp;distID=78&amp;abc3src=link">
<img align="center" style="float: left; margin: 0px 10px 0px 0px;" src="http://www.host.com/abc3/servlet/Download?filename=/medialocker/180/coverart/Album-75.jpg" border="0" ALT="Battleground by Colorado Symphony Orchestra" width="75" height="75">
<br /><br />Battleground by Colorado Symphony Orchestra</a></font>
Mine GO BOOM - Sun Apr 03, 2005 1:20 am
Post subject:
Playing around in Regex Coach, if you have it all be on a single line (the s flag), it works fine. Try that in php real quick. Else, /<A [^>]*HREF=\"(.*?)\"[^>]*>/i works fine.
Anonymous - Sun Apr 03, 2005 9:49 am
Post subject:
hey 2dragons, when are ya gonna be done with the SSI biller? (bump)
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group