ghostwalkz Posted January 6, 2008 Share Posted January 6, 2008 Hi all, I am really in need of a second pair of eyes on this, and a code snippet example would be perfect! I have a long string which is actually html source code. ($html) I need to base64 all the urls within this string. To be more specific.... $html = "This is a test page <a href=\"http://test.com\"/a> blah blah blah <a href = \"http://www.microsoft.com\"/a>"; How can I base64 encode all the urls in this string including the http:// bit? Not jsut the first instance but recursively so all urls are converted within $html?? Heeeeelp me before I throw this laptop thru the window and then drive over it in my car! Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/ Share on other sites More sharing options...
redarrow Posted January 6, 2008 Share Posted January 6, 2008 <?php $str = 'This is an encoded string'; echo base64_encode($str); ?> http://uk3.php.net/base64_encode <?php $str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=='; echo base64_decode($str); ?> http://uk3.php.net/manual/en/function.base64-decode.php Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432097 Share on other sites More sharing options...
ghostwalkz Posted January 6, 2008 Author Share Posted January 6, 2008 Hi thanks for the response, but that will base64 the entire string I need to just get the urls within the string, the good thing is that every url in my string is preceded by "href=" if that helps anybody? Again thanks for response I do need to use that php function tho. Any more takers? Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432099 Share on other sites More sharing options...
GingerRobot Posted January 6, 2008 Share Posted January 6, 2008 Hows about: <?php $str = '<a href="http://www.microsoft.com">microsoft</a> Some text <a href="http://www.google.com">google</a>'; preg_match_all('|<a href="(.*?)">|',$str,$matches); $replacements = array(); foreach($matches[1] as $k => $v){ $replacements[] = base64_encode($v); } $str = str_replace($matches[1],$replacements,$str); echo $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432115 Share on other sites More sharing options...
redarrow Posted January 6, 2008 Share Posted January 6, 2008 <?php $a=array("http://www.google.com","http://www.msn.com"); foreach($a as $b){ $test=base64_encode($b); echo"<a href='$test'>Try me</a><br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432119 Share on other sites More sharing options...
ghostwalkz Posted January 6, 2008 Author Share Posted January 6, 2008 GingerRobot, your code is the closest so far: I have put it up at http://www.weblogica.co.uk/test.php as you can see it kinda seems to just encode the file and not the domain and http:// bit too, its good I could do with a little assistance in order to just kill this one off! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432124 Share on other sites More sharing options...
GingerRobot Posted January 6, 2008 Share Posted January 6, 2008 If you view the source, it's actually doing what you've asked - everything is encoded, including the http:// part. However, the resulting string is still part of a link. Without the http:// prefix, the browser will treat this as a relative link - it thinks that the encoded string is a path to something on your site. I'm not sure what you really wanted to achieve with this encoding, so i cant suggest what to change. Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432127 Share on other sites More sharing options...
redarrow Posted January 6, 2008 Share Posted January 6, 2008 mod_rewrite you need............ .htaccess file Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432130 Share on other sites More sharing options...
GingerRobot Posted January 6, 2008 Share Posted January 6, 2008 Redarrow, sometimes i wonder if you've even paying attention to the question! Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432131 Share on other sites More sharing options...
ghostwalkz Posted January 6, 2008 Author Share Posted January 6, 2008 Hi GingerRobot, again thanks for the help, the html has links that point to other sites which I need to encode and still need them to point the other site. I see what you are saying the encoded string seems local. Can you alter the code to make it so the http:// isnt encoded but the rest of the url is ?? That should do it. Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432134 Share on other sites More sharing options...
ghostwalkz Posted January 6, 2008 Author Share Posted January 6, 2008 maybe add some kind off 'padding' so that 12 characters in it does the base64 encoding? But how to change that code of yours to do that beats me. Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432138 Share on other sites More sharing options...
redarrow Posted January 6, 2008 Share Posted January 6, 2008 gingerobot i do read and i fill modrewrite would off been better Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432140 Share on other sites More sharing options...
GingerRobot Posted January 6, 2008 Share Posted January 6, 2008 I can, but it still wont link to the other site: <?php $str = '<a href="http://www.microsoft.com">microsoft</a> Some text <a href="http://www.google.com">google</a>'; preg_match_all('|<a href="http://(.*?)">|',$str,$matches); $replacements = array(); foreach($matches[1] as $k => $v){ $replacements[] = base64_encode($v); } $str = str_replace($matches[1],$replacements,$str); echo $str; ?> The browser wont decode the string and work out which site it is supposed be going to. Perhaps i should have asked earlier, what are you actually trying to achieve? Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432141 Share on other sites More sharing options...
ghostwalkz Posted January 6, 2008 Author Share Posted January 6, 2008 GingerRobot - Thats it! You cracked it! I know it wont link on my test site, but it will on the real one as the urls are dcoded on the fly during page-parsing thru a proxy. one more thing..... I know its a lot to ask, is it possible for you to slot in a bit of code to urlencode each url before it is encoded in base64? Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432148 Share on other sites More sharing options...
GingerRobot Posted January 6, 2008 Share Posted January 6, 2008 Ah, ok - fair enough. Im pretty sure you could have handled the last bit: <?php $str = '<a href="http://www.microsoft.com">microsoft</a> Some text <a href="http://www.google.com">google</a>'; preg_match_all('|<a href="http://(.*?)">|',$str,$matches); $replacements = array(); foreach($matches[1] as $k => $v){ $replacements[] = base64_encode(urlencode($v)); } $str = str_replace($matches[1],$replacements,$str); echo $str; ?> If thats it all sorted, then don't forget to hit the solved button Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432151 Share on other sites More sharing options...
ghostwalkz Posted January 6, 2008 Author Share Posted January 6, 2008 GingerRobot THANKS! Problem solved, tryed to add you on msn but your listed address failed. Mine is mark*nospam*@*nospam*weblogica.co.uk. ***SOLVED*** Quote Link to comment https://forums.phpfreaks.com/topic/84781-urgent-help-with-base64-encoding-required/#findComment-432153 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.