tgavin Posted January 26, 2008 Share Posted January 26, 2008 <?php $string = 'This is a string with a url in it <a href="http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N">http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N</a>'; echo preg_replace('/(?<=<a href=")(.*?")/', 'http://www.site.com/parse.php?url=\\1', $string); if(isset($_GET['url'])) { echo $_GET['url']; // produces 'http://www.google.co.uk/search?q=php' // instead of the ENTIRE url } ?> I need a way to return the ENTIRE url. Is there a way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/ Share on other sites More sharing options...
amites Posted January 26, 2008 Share Posted January 26, 2008 you might find more help with this one in the regex forum, wish I could help but I'm still rather shaky on my Regex syntax Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-449937 Share on other sites More sharing options...
tgavin Posted January 26, 2008 Author Share Posted January 26, 2008 you might find more help with this one in the regex forum,You're right. I didn't even know that forum existed. I don't want to go posting the same thing in another forum though. maybe an admin could move it? Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-449954 Share on other sites More sharing options...
resago Posted January 26, 2008 Share Posted January 26, 2008 this is a little more clear preg_replace('/<a href="(.*)">/', '<a href="http://www.site.com/parse.php?url=\\1">', $string); Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-449995 Share on other sites More sharing options...
tgavin Posted January 26, 2008 Author Share Posted January 26, 2008 this is a little more clear preg_replace('/<a href="(.*)">/', '<a href="http://www.site.com/parse.php?url=\\1">', $string); Thank you. That's only returning the url (and it's still chopped off). I need to be able to return the entire string, with the url modified. What I have above works, except that it's chopping off the url at the first ampersand. Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-450000 Share on other sites More sharing options...
resago Posted January 26, 2008 Share Posted January 26, 2008 my code got cut off, its: preg_replace('/<a href="(.*)">/', "<a href='http://www.site.com/parse.php?url=$1'>", $string); Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-450017 Share on other sites More sharing options...
tgavin Posted January 26, 2008 Author Share Posted January 26, 2008 my code got cut off, its: preg_replace('/<a href="(.*)">/', "<a href='http://www.site.com/parse.php?url=$1'>", $string); Yep, that's cleaner I'm still encountering a problem, and I'm thinking it's more in the receiving end. When I run that regex it outputs a link in the browser. When I click that link it goes to my parse.php script - which is exactly what it's supposed to do. In the script I echo $_GET['url'], which chops off the url at the first ampersand. How can I go about getting the entire url? Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-450021 Share on other sites More sharing options...
laffin Posted January 26, 2008 Share Posted January 26, 2008 ahhh, cuz it thinks the parameters are for itself. if u run do a print_r($_GET) in parse.php u will see what i mean. you can either rebuild the paramters in parse.php, not shure if u can use urlencode on this, might be worth a shot. Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-450042 Share on other sites More sharing options...
resago Posted January 26, 2008 Share Posted January 26, 2008 what if you just base64encode it, then decode it in the target php? Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-450064 Share on other sites More sharing options...
tgavin Posted January 26, 2008 Author Share Posted January 26, 2008 what if you just base64encode it, then decode it in the target php? I originally tried url_encode() and received errors. However, it's entirely likely that I didn't know what I was doing and put it in the wrong place. Could you give me an example of base64? I'm slightly familiar with it, but have never used it. Also, would I have to change the document encoding as well? If that doesn't work, I wrote this per laffin's suggestion about rebuilding - which is a pretty good suggestion. <?php foreach($_GET as $name=>$value) { $param .= $name.'='.$value; $param .= '&'; } $url = str_replace('url=','',$param); echo $url; ?> See any (future) problems with that? Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-450072 Share on other sites More sharing options...
laffin Posted January 26, 2008 Share Posted January 26, 2008 <?php $url=$_GET['url']; unset($_GET['url']); foreach($_GET as $name=>$value) { $param[] = $name.'='.$value; } $url .= "?" . implode("&",$param); echo $url; ?> Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-450075 Share on other sites More sharing options...
resago Posted January 26, 2008 Share Posted January 26, 2008 my code got cut off, its: preg_replace('/<a href="(.*)">/', "<a href='http://www.site.com/parse.php?url=".base64_encode($1)."'>", $string); then in your php $realurl=base64_decode($_GET['url']); whatever works for you. Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-450076 Share on other sites More sharing options...
laffin Posted January 26, 2008 Share Posted January 26, 2008 heh, I like that method as well. Thanks for the tip Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-450087 Share on other sites More sharing options...
tgavin Posted January 26, 2008 Author Share Posted January 26, 2008 my code got cut off, its: preg_replace('/<a href="(.*)">/', "<a href='http://www.site.com/parse.php?url=".base64_encode($1)."'>", $string); then in your php $realurl=base64_decode($_GET['url']); whatever works for you. That's basically what I tried before with url_encode and received an error similar to what I get with yours: Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-450118 Share on other sites More sharing options...
resago Posted January 27, 2008 Share Posted January 27, 2008 guess you need to force it into a string. Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-450137 Share on other sites More sharing options...
tgavin Posted January 27, 2008 Author Share Posted January 27, 2008 guess you need to force it into a string. at the risk of becoming a pain in the butt, I'm not sure how to do that. Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-450155 Share on other sites More sharing options...
laffin Posted January 28, 2008 Share Posted January 28, 2008 preg_replace('/<a href="(.*)">/', "<a href='http://www.site.com/parse.php?url=".base64_encode($1)."'>", $string); i dun see how this works, preg replace needs the 2nd paramenter to be a string beforehand. it will not parse base64 while performing the repacement. <?php header("Content-type: text/plain"); $body='This is a string with a url in it <a href="http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N">http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N</a>'; echo $body."\n"; function urlencodebase64($matches) { $url=urlencode(base64_encode($matches[2])); return "<A HREF=\"http://my.site.com/parse.php?url=". $url ."\">"; } $body=preg_replace_callback('/(<a\s+.*href\s*=\s*"(.*)".*?>)/i','urlencodebase64',$body); echo $body."\n"; ?> which outputs This is a string with a url in it <a href="http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N">http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N</a> This is a string with a url in it <A HREF="http://my.site.com/parse.php?url=aHR0cDovL3d3dy5nb29nbGUuY28udWsvc2VhcmNoP3E9cGhwJm51bT0xMDAmaGw9ZW4mc2FmZT1vZmYmc3RhcnQ9MjAwJnNhPU4%3D">http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N</a>[/qyote[ why use urlencode & base64? base64 may use some characters that a wont work on a url parameter line. function urldecodebase64($url) { return base64_decode($urldecode($url)); } Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-451026 Share on other sites More sharing options...
tgavin Posted January 28, 2008 Author Share Posted January 28, 2008 Thanks! I was looking at preg_replace_callback last night, but had nothing like this Good to know that about base64_encode! I'm real close now. The only problem is the matching pattern. If I have more than one url in the string (which is *very* likely), it will only return the last one. The original pattern I had worked. It won't work with this though. <?php function urlencodebase64($matches){ $url = urlencode(base64_encode($matches[2])); return "<a href=\"http://www.ballywhonews.com/dev/test/testlink.php?url=".$url."\">"; } function urldecodebase64($url) { return base64_decode(urldecode($url)); } // put it together $string = 'This is a string with a url in it <a href="http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N">http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N</a><p>Not only that, but here is another one <a href="http://www.aol.com">America Online</a></p>'; $new_string = preg_replace_callback('/(<a\s+.*href\s*=\s*"(.*)".*?>)/i','urlencodebase64',$string); // echo it so we can click the links for testing echo $new_string; // after the link is clicked and the page is reloaded if(isset($_GET['url'])) { $url = urldecodebase64($_GET['url']); echo '<p>'.$url; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-451310 Share on other sites More sharing options...
laffin Posted January 28, 2008 Share Posted January 28, 2008 <?php function urlencodebase64($matches){ $url = urlencode(base64_encode($matches[2])); return "<a href=\"http://www.ballywhonews.com/dev/test/testlink.php?url=".$url."\">"; } function urldecodebase64($url) { return base64_decode(urldecode($url)); } // put it together $string = 'This is a string with a url in it <a href="http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N">http://www.google.co.uk/search?q=php&num=100&hl=en&safe=off&start=200&sa=N</a><p>Not only that, but here is another one <a href="http://www.aol.com">America Online</a></p>'; $new_string = preg_replace_callback('/(<a\s+.*?href\s*=\s*"([^\s"]*)".*?>))/i','urlencodebase64',$string); // echo it so we can click the links for testing header("Content-type: text/plain"); echo "$string\n"; echo "$new_string\n"; // after the link is clicked and the page is reloaded if(isset($_GET['url'])) { $url = urldecodebase64($_GET['url']); echo '<p>'.$url; } ?> changed the preg statement a little Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-451322 Share on other sites More sharing options...
tgavin Posted January 28, 2008 Author Share Posted January 28, 2008 Thank you! I received this error preg_replace_callback() [function.preg-replace-callback]: Compilation failed: unmatched parentheses at offset 36 So I removed the last parentheses and ended up with this $new_string = preg_replace_callback('/(<a\s+.*?href\s*=\s*"([^\s"]*)".*?>)/i','urlencodebase64',$string); If that looks good to you too, then I'm all set! Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-451459 Share on other sites More sharing options...
laffin Posted January 28, 2008 Share Posted January 28, 2008 looks good to me. prolly a copy and paste error on my part. Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-451468 Share on other sites More sharing options...
tgavin Posted January 28, 2008 Author Share Posted January 28, 2008 Thank you very much for your help. I really appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-451473 Share on other sites More sharing options...
laffin Posted January 28, 2008 Share Posted January 28, 2008 no problem, it's good to test one skills against problems/challenges posed by others. it keeps yer skills developing Quote Link to comment https://forums.phpfreaks.com/topic/87941-solved-finding-and-replacing-a-url/#findComment-451486 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.