Synook Posted September 27, 2007 Share Posted September 27, 2007 Hello all,I have another problem with a REGEX. I want to replace all hyperlink markups in a string to add ?q= to the front of the href, using preg_replace, e.g. <a href="http://www.google.com"> to <a href="?q=http://www.google.com">. But I can't do it :)Any ideas? Link to comment Share on other sites More sharing options...
Synook Posted September 27, 2007 Author Share Posted September 27, 2007 No what the... i've solved my problem again! Why does this always happen - 3 days frustration, I post, 10 minutes later - problem solved :)But - I still have another problem. I want to make it so that if the href=, say, "index.html" and I am at http://www.google.com, it will convert the href to http://www.google.com/index.html. But, if the original href is http://www.yahoo.com, then it will convert it to ?q=http://www.yahoo.com and not?q=http://www.google.comhttp://www.yahoo.comSo, any ideas on that one? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 27, 2007 Share Posted September 27, 2007 You need to check for the scheme:// in the link, if it's not there then it's a relative link or else it's an absolute link. What do you have so far? Link to comment Share on other sites More sharing options...
Synook Posted September 27, 2007 Author Share Posted September 27, 2007 $page = preg_replace("<a (.*?)href=\"(.*?)\"(.*?)>", "a $1" . "href=\"?address={$address}$2\"$3", $page); (?q has been changed to ?address)The problem is, is there a way of doing conditional in REGEX? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 27, 2007 Share Posted September 27, 2007 Yes, there is a conditional operator in PCRE. Check the reference page:http://www.php.net/manual/en/reference.pcr...tern.syntax.php Link to comment Share on other sites More sharing options...
Synook Posted September 28, 2007 Author Share Posted September 28, 2007 Oh - I see... but what about for the replacement argument? I don't see how I could use that. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.