PHP string decoding

I am working on a PHP application that needs to parse strings sent by another program. the problem is that some lines have octal characters and some other screens in the middle.

So, instead script>XYZ, I get:

\103RI\120T>XYZ%6En \151\156 d%6Fcu\155%65n..

And I need to print this line, decoded ... I tried to use octdec, url_decodeetc., but one only works with one char, and the other does not decode the octal ... Does anyone have any suggestions?

+5
source share
4 answers

Try the following:

$str = '\103RI\120T>XYZ%6En \151\156 d%6Fcu\155%65n..';

// CRIPT>XYZnn in documen..
echo preg_replace(array('~\\\(\d+)~e', '~%([0-9A-F]{2})~e'), array('chr(octdec("$1"))', 'chr(hexdec("$1"))'), $str);

As for the parts %AD, I'm not sure that they are meant to be presented, could you explain?

+1
urldecode(stripcslashes("\103RI\120T>XYZ%6En \151\156 d%6Fcu\155%65n.."));
+1

preg_replace_callback(). , , ( \ %. , , , escape-.

, base_convert() (base_convert($match, 8, 10) ; base_convert($match, 16, 10) ).

+1
$octstr = '\103RI\120T>XYZ%6En \151\156 d%6Fcu\155%65n';

preg_match_all('/\\\[0-9]{3}/',$octstr,$matches);

$oct = $matches[0];

foreach($oct as $o){
    $octstr = str_replace($o,chr(octdec($o)),$octstr);
}

echo urldecode($octstr);

:

CRIPT>XYZnn in documen
0

All Articles