Convert XML UTF-16 to JSON UTF-8 with PHP cURL
- Category: Personal Home Page
- Hits: 61433
Fix PHP cURL: parser error: Document labelled UTF-16 but has UTF-8 content
What?
This is an article with notes for me on how to convert some received XML encoded in UTF-16 to some JSON in UTF-8. If it were entirely in UTF-8, I would simply load the received XML with SimpleXML and use the built-in PHP JSON_encode function. I ran into the following errors:
Warning: SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]: Entity: line 1: parser error : Document labelled UTF-16 but has UTF-8 content in /public_html/.../.../my_script.php on line ###
Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: Entity: line 1: parser error : Document labelled UTF-16 but has UTF-8 content in /public_html/.../.../my_script.php on line ###Why?
So I've googled, binged and yahoo'd for this and although there are some solutions that deal with loading UTF16 content into SimpleXMLElement or simplexml_load_string, it doesn't solve my problem. I'm receiving XML data within a cURL result but I get the above error with using either "SimpleXMLElement" or "simplexml_load_string". Returning the XML with cURL isn't a problem, but I want to convert it to JSON and I usually use a PHP function to load the data into an XML array and use the built-in PHP function: "json_encode".
How?
PHP: First name and Initial of Surname
- Category: Personal Home Page
- Hits: 18568
A note for myself on some code to convert a string of two names into a string made up of the first name and then using the initial of the second name.
-- What I have John Smith Fred.Bloggs -- What I want John S. Fred B.
How?
htaccess Rewrites SEF URL and submits to PHP
- Category: Personal Home Page
- Hits: 30043
A quick note on a htaccess rewrite rule I'm liking.
What does it do?
What I type:
http://www.mywebsite.com/blog/videos.htmlSends this to server:
http://www.mywebsite.com/index.php?myFolder=blog&myFiles=videosHow?
Preg_Replace all strings between two tags
- Category: Personal Home Page
- Hits: 61154
For those of you who use Preg_Replace. Preg_replace is a function that uses regular expressions to search and replace a string.
Why?
Because my understanding with regular expressions is shady and varies from language to language, I've written this article as a quick reference point.
How?
Php convert filesizes to bytes kb mb gb
- Category: Personal Home Page
- Hits: 23924
Just a quick note on how to format a given filesize and to reduce the display output to a small string, eg:
196 bytes : displays as => "196 bytes" 12945 bytes : displays as => "12 Kb" 1478515 bytes : displays as => "1 Mb" 8798745455 bytes : displays as => "8 Gb"
Convert seconds to total time in PHP
- Category: Personal Home Page
- Hits: 14994
This is intended for activity/session durations and although I usually get MySQL to do the date/time calculations, there are times when we have to do with PHP. This is the shortest way I know to properly convert seconds into total hours, minutes and seconds (taking into account regional settings and without using a date function).
How?
$total_time =intval(intval($total_seconds)/ 3600).":"; $total_time.=str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":"; $total_time.=str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT); // yields // 82800 = 23:00:00 // 108000 = 30:00:00On one line:
$total_time=intval(intval($total_seconds)/ 3600).":".str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":".str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT);
First and Last Entry on a Page using Modulus Remainder
- Category: Personal Home Page
- Hits: 22962
This article is a quick note (so I never spend as long again) in PHP on how to determine when looping through a loop, which entry was first and which was last. This is incredibly useful for pagination.
PHP & MySQL Search Engine
- Category: Personal Home Page
- Hits: 54471
So we want to give a search engine to our users. This sounds really simple, we could try:
$search_term_esc = AddSlashes($search_term); $sql = "SELECT * FROM Content WHERE content_body LIKE '%$search_term_esc%'";Great! Few problems though, multiple terms are not supported; quotation marks and apostrophes may be an issue;

