Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
PHP regex

 
Post new topic   Reply to topic Printable version
 View previous topic  So, you wanna optomize your loop, huh? Post :: Post ....Agurus, are you there?  View next topic  
Author Message
2dragons
Novice


Joined: Feb 17 2004
Posts: 95
Offline

PostPosted: Sat Apr 02, 2005 10:25 pm   Post maybe stupid    Post subject: PHP regex Reply to topic Reply with quote

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?
Back to top
View users profile Send private message Add User to Ignore List
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:42
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3615
Location: Las Vegas
Offline

PostPosted: Sat Apr 02, 2005 10:35 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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;
?>
Back to top
View users profile Send private message Add User to Ignore List Send email
2dragons
Novice


Joined: Feb 17 2004
Posts: 95
Offline

PostPosted: Sat Apr 02, 2005 10:54 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:42
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3615
Location: Las Vegas
Offline

PostPosted: Sun Apr 03, 2005 12:04 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

Worked fine for me when tried it out. The server I tried it on is running PHP/4.3.10.
Back to top
View users profile Send private message Add User to Ignore List Send email
Cerium
Server Help Squatter


Age:43
Gender:Gender:Male
Joined: Mar 05 2005
Posts: 807
Location: I will stab you.
Offline

PostPosted: Sun Apr 03, 2005 12:25 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List AIM Address
2dragons
Novice


Joined: Feb 17 2004
Posts: 95
Offline

PostPosted: Sun Apr 03, 2005 1:05 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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>
Back to top
View users profile Send private message Add User to Ignore List
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:42
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3615
Location: Las Vegas
Offline

PostPosted: Sun Apr 03, 2005 1:20 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List Send email
Chambahs
Guest


Offline

PostPosted: Sun Apr 03, 2005 9:49 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

hey 2dragons, when are ya gonna be done with the SSI biller? (bump)
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Trash Talk All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 66 page(s) served in previous 5 minutes.

phpBB Created this page in 0.606917 seconds : 32 queries executed (88.4%): GZIP compression disabled