Jump to content

Recommended Posts

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!

 

 

 

 

<?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

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?

 

 

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;
?>

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

 

 

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.

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.

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?

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?

 

 

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 :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.