I haven't been able to figure out how to replace "h" with "h", "&x69;" with "i", etc.
Anyone want to offer some help?
Replace 'h' with 'h'
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Replace 'h' with 'h'
Here's a hint:nigma wrote:I haven't been able to figure out how to replace "h" with "h", "&x69;" with "i", etc.
Anyone want to offer some help?
Code: Select all
echo chr(0x68);Code: Select all
function make_ascii($chr)
{
$chr = $chr[1];
if (substr($chr, 0, 1) == 'x')
{
$chr = '0'.$chr;
}
return chr($chr);
}
echo preg_replace_callback('@&#([a-z0-9]+);@is', 'make_ascii', $string);- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Assuming ruby has some PCRE regex handling, a way to evaluate basic code and a way to get substrings....
Code: Select all
<?php
$string = "i & also; x h";
preg_match_all('@(&#x[a-f0-9]+;|.)@is', $string, $matches); //Any *global* search
print_r($matches);
$str = '';
for ($i=0; $i<count($matches[1]); $i++) //All the 1st backreferences
{
if (preg_match('/^&#x[a-f0-9]+;$/i', $matches[1][$i])) //An entity
{
$hex = substr($matches[1][$i], 2, -1); //Remove the syntax
$tmp = '\\'.$hex; //Build a string
$str .= eval("return \"$tmp\";"); //Evaluate it
}
else
{
$str .= $matches[1][$i]; //Doesn't need parsing
}
}
echo $str;
?>