Jump to content

[SOLVED] Base64 Encode all instances of a $string within a $string


ghostwalkz

Recommended Posts

Hi all,

 

It will be better to show you what I mean. Below is the 'string' (for example).

 

We'll call it '$string1'

 

What I want to do is Base64 encode everything between the href=" and the following quote (ie the url), not just once but throughout '$string1'

 

Any Ideas? An exact code snippet would really be appreciated!

 

snip--------

 

<link rel="shortcut icon" href="/cms/images/favicon.ico" />

<link href="/cms/templates/rt_versatility4_j15/css/template_css.css" rel="stylesheet" type="text/css" />

<link href="/cms/templates/rt_versatility4_j15/css/menustyle4.css" rel="stylesheet" type="text/css" />

<link href="/cms/templates/rt_versatility4_j15/css/style2.css" rel="stylesheet" type="text/css" />

<link href="/cms/templates/rt_versatility4_j15/css/typography.css" rel="stylesheet" type="text/css" />

<link href="/cms/templates/system/css/system.css" rel="stylesheet" type="text/css" />

 

<link href="/cms/templates/system/css/general.css" rel="stylesheet" type="text/css" />

<link href="/cms/templates/rt_versatility4_j15/css/rokslidestrip.css" rel="stylesheet" type="text/css" />

<link href="/cms/templates/rt_versatility4_j15/css/rokmoomenu.css" rel="stylesheet" type="text/css" />

 

 

 

unsnip--------

try

<?php
$test = '<link rel="shortcut icon" href="/cms/images/favicon.ico" />
<link href="/cms/templates/rt_versatility4_j15/css/template_css.css" rel="stylesheet" type="text/css" />
<link href="/cms/templates/rt_versatility4_j15/css/menustyle4.css" rel="stylesheet" type="text/css" />
<link href="/cms/templates/rt_versatility4_j15/css/style2.css" rel="stylesheet" type="text/css" />
<link href="/cms/templates/rt_versatility4_j15/css/typography.css" rel="stylesheet" type="text/css" />
<link href="/cms/templates/system/css/system.css" rel="stylesheet" type="text/css" />

<link href="/cms/templates/system/css/general.css" rel="stylesheet" type="text/css" />
<link href="/cms/templates/rt_versatility4_j15/css/rokslidestrip.css" rel="stylesheet" type="text/css" />
<link href="/cms/templates/rt_versatility4_j15/css/rokmoomenu.css" rel="stylesheet" type="text/css" />
';
echo preg_replace_callback('/(\shref=")(.*?)(")/is', 'code', $test);
function code($x){
return $x[1].base64_encode($x[2]).$x[3];
}
?>

This works perfectly for me:

<?php
$test = '<link rel="shortcut icon" href="/cms/images/favicon.ico" />
<link href="/cms/templates/rt_versatility4_j15/css/template_css.css" rel="stylesheet" type="text/css" />
<link href="/cms/templates/rt_versatility4_j15/css/menustyle4.css" rel="stylesheet" type="text/css" />
<link href="/cms/templates/rt_versatility4_j15/css/style2.css" rel="stylesheet" type="text/css" />
<link href="/cms/templates/rt_versatility4_j15/css/typography.css" rel="stylesheet" type="text/css" />
<link href="/cms/templates/system/css/system.css" rel="stylesheet" type="text/css" />

<link href="/cms/templates/system/css/general.css" rel="stylesheet" type="text/css" />
<link href="/cms/templates/rt_versatility4_j15/css/rokslidestrip.css" rel="stylesheet" type="text/css" />
<link href="/cms/templates/rt_versatility4_j15/css/rokmoomenu.css" rel="stylesheet" type="text/css" />
';
$test = preg_replace('/(?<=\bhref=")([^"]+)(?=")/e', 'base64_encode("$1");', $test);
echo $test;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.