php how to decode a text file that contains chars like \u0003c? - php

When I run
$result = exec("curl someURL");
I got results that contains \u0003 etc.. chars:
"d":"\u003c?xml version=\"1.0\"?\u003e\u003cArrayOfAnnoncePresentation
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\u003e\u003cPageCount\u003e\u003cPageCount\u003e25\u003c/PageCount\u003e\u003c/PageCount\u003e\u003cAnnoncePresentation\u003e\u003cTitre\u003e3garages\u003c/Titre\u003e\u003cLienDetail\u003e/
How can I decode that ?

Related

PHP get base64 decoding xml array and encode it to pdf

I need to convert XML array (contained base64 decoding) and encode it to PDF.
This is the array:
<response>
<xmlArray>
<blabla>TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gTnVuYyB2ZW5lbmF0aXMsIGp
1c3RvIHV0IGF1Y3RvciBzZW1wZXIsIHB1cnVzIGxlY3R1cyBlbGVtZW50dW0gbGliZXJvLCBhYyBwZWxsZW50ZXNxdWUgYW50ZSBqdXN0b
yBldCB0dXJwaXMuIE51bmMgYmliZW5kdW0gZWdlc3RhcyBkb2xvciB2b2x1dHBhdCBlZ2VzdGFzLiBTdXNwZW5kaXNzZSBkYXBpYnVzIHN
lbSBvZGlvLCBpbiBmYXVjaWJ1cyBsZWN0dXMgcHVsdmluYXIgdml0YWUuIE5hbSBtYXR0aXMgZXVpc21vZCBhdWd1ZSwgZWdldCBmaW5pY
nVzIGxlbyBoZW5kcmVyaXQgbmVjLiBDbGFzcyBhcHRlbnQgdGFjaXRpIHNvY2lvc3F1IGFkIGxpdG9yYSB0b3JxdWVudCBwZXIgY29udWJ
pYSBub3N0cmEsIHBlciBpbmNlcHRvcyBoaW1lbmFlb3MuIE51bmMgaWQgbnVuYyBsZWN0dXMuIFBlbGxlbnRlc3F1ZSBsYWN1cyB1cm5hL
CB2aXZlcnJhIGNvbnZhbGxpcyBlZmZpY2l0dXIgdmVsLCBhbGlxdWFtIHNpdCBhbWV0IGRpYW0uIEluIGEgYWxpcXVldCBtYXNzYS4gU2V
udCwgdGVtcHVzIGhlbmRyZXJpdCBlbmltIGZhdWNpYnVzLiBEb25lYyBtYXR0aXMgZWxpdCBub24gbWFzc2EgaW50ZXJkdW0gZmF1Y2lid
XMuIEFlbmVhbiBub24gbWF1cmlzIGluIHVybmEgbWFsZXN1YWRhIGx1Y3R1cy4=
</blabla>
</xmlArray>
</response>
My attempt up until now is
<?php
// 1st. convert xml array; get the blabla array information
$blabla2 = $xml->xmlArray->blabla;
// remove CR, new lines and whitespace on blabla2
$blabla3 = str_replace(array("\n", "\t", "\r"), '', $blabla2);
// 2nd. encode it to PDF
header('Content-type: application/pdf');
$blabla4 = base64_decode($blabla3 );
echo $blabla4 ;
?>
The result is a pdf, but not the way I expected as it shows this line on pdf:
%PDF-1.410obj<</Title(þÿ)/Creator(þÿwkhtmltopdf0.12.2.4) ...
Would you mind to tell me how to show the pdf properly?
Thanks a lot!

Wrapping String PHP

I have a problem with my code, i have this code that create image from external source of image & string. I used json to get the string.
My problem is if i used the string from json data i could not get the proper wrapping of string like this:
http://prntscr.com/dbhg4n
$url = 'https://bible-api.com/Psalm100:4-5?translation=kjv';
$JSON = file_get_contents($url);
$data = json_decode($JSON);
$string = $data->text;
But if i declare and set string directly i got the output that i want like this:
http://prntscr.com/dbhg7q
$string = "Enter into his gates with thanksgiving, and into his courts with praise: be thankful unto him, and bless his name. For the Lord is good; his mercy is everlasting; and his truth endureth to all generations.";
I dont think the error or the problem is on the code for wrapping the text on my image. I think it is on the json data. How can i fix this?
The text has \n symblols. Just replace them:
$string = preg_replace("/\n/", ' ', $data->text);
or without a regular expression:
$string = str_replace("\n", ' ', $data->text);

SimpleXMLElement set value to special characters

I'm trying to send special characters (less than, greater than) to the text of a node using SimpleXMLElement, but it's converting it to the escaped values.
$header[0] = "%SET(Amt_,<AMT>". $amt . "</AMT>) \n" .$header[0];
The above results in the following in the XML file:
%SET(Amt_,<AMT>100</AMT>)
I tried using html_entity_decode and it still wrote to file the same way. Is there any way to write 'special' characters to the text value of a simplexmlelement object?
clarification: I want to write the actual characters '<' and '>' to the file when $header->asXML() is called. Currently the escaped versions are what is written to file.
Use htmlspecialchars while sending data and htmlspecialchars_decode whenever you want to get the original string.
<?php
$amt = 100;
$header[0] = "Some Value";
$header[0] = "%SET(Amt_,<AMT>". $amt . "</AMT>) \n" .$header[0];
$node = htmlspecialchars($header[0]);
$value = htmlspecialchars_decode($node);
file_put_contents("filename.txt", $value);
echo "written on file";
?>
A work around that works for my case, I reopened the file contents, decoded it and resaved it to file.
$xml->asXML('test.xml');
$coded = file_get_contents('test.xml');
file_put_contents('test.xml', htmlspecialchars_decode($coded), LOCK_EX);

PHP replace : find and replace the same characters with different text

How can I find and replace the same characters in a string with two different characters? I.E. The first occurrence with one character, and the second one with another character, for the entire string in one go?
This is what I'm trying to do (so users need not type html in the body): I've used preg_replace here, but I'll willing to use anything else.
$str = $str = '>>Hello, this is code>> Here is some text >>This is more code>>';
$str = preg_replace('#[>>]+#','[code]',$str);
echo $str;
//output from the above
//[code]Hello, this is code[code] Here is some text [code]This is more code[code]
//expected output
//[code]Hello, this is code[/code] Here is some text [code]This is more code[/code]
But problem here is, both >> get replaced with [code]. Is it possible to somehow replace the first >> with [code] and the second >> with a [/code] for the entire output?
Does php have something to do this in one go? How can this be done?
$str = '>>Hello, this is code>> Here is some text >>This is more code>>';
echo preg_replace( "#>>([^>]+)>>#", "[code]$1[/code]", $str );
The above will fail if something like the following is your input:
>>Here is code >to break >stuff>>
To deal with this, use negative lookahead:
#>>((?!>[^>]).+?)>>#
will be your pattern.
echo preg_replace( "#>>((?!>[^>]).+?)>>#", "[code]$1[/code]", $str );

Decode base64 string - php

Is there any way to decode this string??
Actual string : 其他語言測試 - testing
base64 encode while sending on mail as subject as
"=?iso-2022-jp?B?GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==?="
<?php
echo base64_decode('GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==');
?>
This is base 64 encode, I couldn't decode it to actual Chinese string.Since it has been encoded using "iso-2022-jp", I have also tried online base64decode.org site to decode this string, but I couldn't find the original string, how can I do that?
Use iconv():
<?php
$input = base64_decode('GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==');//$BB6B>8l8#B,;n(B - testing
$input_encoding = 'iso-2022-jp';
echo iconv($input_encoding, 'UTF-8', $input); //其他語言測試 - testing
?>
What you are looking at is MIME header encoding. It can be decoded by mb_decode_mimeheader(), and generated by mb_encode_mimeheader(). For example:
<?php
mb_internal_encoding("utf-8");
$subj = "=?iso-2022-jp?B?GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==?=";
print mb_decode_mimeheader($subj);
?>
其他語言測試 - testing
(The call to mb_internal_encoding() is necessary here because the contents of the subject line can't be represented in the default internal encoding of ISO8859-1.)
Try encoding the string to UTF-8 first and then encode it to base 64.
Same when decoding, decode the string from base64 and then from UTF-8.
This is working for me:
php > $base = "其他語言測試 - testing";
php > $encoded = base64_encode(utf8_encode($base));
php > $decoded = utf8_decode(base64_decode($encoded));
php > echo ($decoded === $base) . "\n";
1

Categories