I am trying to send an encoded string appending it to an api url used fo sending BULK SMS. But not all characeters are being encoded correctly. I've tried using utf8_encode, htmlentities and htmlspecialchars on the string before using urlencode but that didn't work either. Since its an external api call they are only using urldecode for showing the string which I can not change. So what needs to be done to encode the special characters present in the following string to show it correctly in the resulting SMS.
Example String:
$message = '2016 brings with it a new year sale at “Orchid” & “home n décor”.';
2016 brings with it a new year sale at “Orchid” & “home n décor”.
After applying urlencode and htmlentities
urlencode(htmlentities($message, ENT_QUOTES, 'UTF-8'))
I get the following output -
2016+brings+with+it+a+new+year+sale+at+%26ldquo%3BOrchid%26rdquo%3B+%26amp%3B+%26ldquo%3Bhome+n+d%26eacute%3Bcor%26rdquo%3B.
When I try to use urldecode on this it gives me result as expected -
2016 brings with it a new year sale at “Orchid” & “home n décor”.
When I try to send the encoded output string as an url parameter and echo the parameter I get the exact same result, without even using the urldecode.
But the problem is in my SMS body, it is always showing in the following pattern -
2016 brings with it a new year sale at “Orchid” & “home n décor”.
Why is it not showing the decoded string in SMS? How this can be done? Its showing normal encoded characters like & without any problem as its encoded to %26. But causing issues for double quotes or characters like é. Can anyone suggest any workarounds?
Use:
$message = "Message Text";
instead of:
$message = urlencode("Message Text");
Related
I'm trying to use PHP function file_get_contents() on this url: http://www.omdbapi.com/?i=tt0460681 which should return a JSON object.
The Year returns as 2005†when its suppose to return as 2005-, which I find really random.
I have tried to convert the encoding of my document betweem UTF8 and ASCII to see if it was simply outputted wrong, but this has had no effect.
The API works correctly, it sends a header specifying the encoding of the JSON data:
Content-Type: application/json; charset=utf-8
But file_get_contents() doesn't relay that information. PHP just assumes all data uses some 8-bit character encoding. So the returned string will just contain the sequence of UTF-8 encoded bytes returned by the server.
Since PHP throws away the encoding information, you have to make an assumption here: it's probably safe to assume the API always uses UTF-8 to encode the text:
Option 1 (the one I would recommend): change the encoding for your HTML output to UTF-8. You should then change your web server settings so it specifies that encoding in the Content-Type header. echo $content will then give the expected result. But it requires you change the rest of your PHP code to output proper UTF-8.
Option 2: use the htmlentities function to convert the characters to entities. Try this: htmlentities($content, ENT_COMPAT | ENT_HTML401, "utf-8")
If you don't know for sure what encoding the API will use, you'll have to use a module like curl, which allows you to inspect the response headers sent by the API.
- and – are two different characters.
The first one is know as en dash whereas the second is called hyphen-minus.
Here is glyph, unicode, htmlentity and name of the two.
– | U+2013 | – | hyphen-minus
- | U+002D | - | en dash
So, the problem is with the API not sending the proper value with proper encoding. Because, it's sending you the invalid first character – instead of the second one.
A quick solution for this would be to convert the string manually as
$content = str_replace('â€', '-', $content);
You could try to sanitize the field content, removing all non-numeric chars. For example:
$year = preg_replace("/\\D/i", '', $responseObject['Year']);
You could try converting the string to uft8 directly in the php code using
uft8_decode($string)
and
uft8_encode($string)
I have a webform in php that sends 2 emails. One is in plaintext, one in html to the customer that includes a url string based on the user input.
If the name field for instance says "Bob O'Reilly", the plaintext email is fine but the html email string would read "http://www.mysite.com?name=bob o", completely truncating the string. I know I need to escape the apostrophe but I've tried addslashes and it doesn't seem to do what I need it to.
Thanks
You should probably urlencode the string when using it in a link: http://php.net/manual/en/function.urlencode.php.
This transforms any characters that may have some special meaning to a code like %20 (space). You can use urldecode if you need to accept a requested, urlencoded string.
I am pulling data from the Facebook graph which has characters encoded like so: \u2014 and \u2014
Is there a function to convert those characters into HTML? i.e \u2014 -> —
If you have some further reading on these character codes), or suggested reading about unicode in general I would appreciate it. This is so confusing to me. I don't know what to call these codes... I guess unicode, but unicode seems to mean a whole lot of things.
that's not entirely true bobince.
How do you handle json containing spanish accents?
there are 2 problems.
I make FB.api(url, function(response)
... var s=JSON.stringify(response);
and pass it to a php script via $.post
First I get a truncated string. I need escape(JSON.stringify(response))
Then I get a full json encoded string with spanish accents.
As a test, I place it in a text file I load with file_get_contents and apply php json_decode and get nothing.
You first need utf8_encode.
And then you get awaiting object of your desire.
After a full day of test and google without any result when decoding unicode properly, I found your post.
So many thanks to you.
Someone asked me to solve the problem of Arabic texts from the Facebook JSON archive, maybe this code helps someone who searches for reading Arabic texts from Facebook (or instagram) JSON:
$str = '\u00d8\u00ae\u00d9\u0084\u00d8\u00b5';
function decode_encoded_utf8($string){
return preg_replace_callback('#\\\\u([0-9a-f]{4})#ism', function($matches) { return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE"); }, $string);
}
echo iconv("UTF-8", "ISO-8859-1//TRANSLIT", decode_encoded_utf8($str));
Facebook Graph API returns JSON objects. Use json_decode() to read them into PHP and you do not have to worry about handling string literal escapes like \uNNNN. Don't try to decode JSON/JavaScript string literals by yourself, or extract chosen properties using regex.
Having read the string value, you'll have a UTF-8-encoded string. If your target HTML is also UTF-8-encoded, you don't need to replace — (U+2014) with any entity reference. Just use htmlspecialchars() on the string when outputting it, so that any < or & characters in the string are properly encoded.
If you do for some reason need to produce ASCII-safe HTML, use htmlentities() with the charset arg set to 'utf-8'.
I sent a text via GET method to decode html entities ( w = w )
> ?text=w&type=htmldecode&format=text
I got errors in the $text variable then I tried to set it in the last of the link
?format=text&type=htmldecode&text=w
and I got the same errors
how I can fix that ?
There are 2 types of encoding pertinent to your problem. HTML escape characters, and URL escape chars.
When you have a character in an HTML page, you use the HTML escape characters. eg
w = w
But you cannot use those characters in a URL - & and # have special meanings in URLs. So you have to encode again - this time using URL escape characters.
# = %23
& = %26
; = %3B
So your string, ('w') fit to be put into a URL, would be:
%23%26119%3B
and your entire query string:
?text=%23%26119%3B&type=htmldecode&format=text
the aforementioned PHP urlencode() does this.
The snippet:
<?php echo urlencode("w"); ?>
outputs
%26%23119%3B
I think you need to decode it then re-encode it using URL encoding urlencode
I am attempting to open a page with window.open and it's not working. The path shown is like xyz/a%20b%20c%20.pdf, but it is supposed to be xyz/abc.pdf. If I remove the % and 20 manually, it works, how can I remove these characters using PHP?
Use urldecode:
(PHP 4, PHP 5)
urldecode — Decodes URL-encoded string
Description
string urldecode ( string $str )
Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
Example
echo urldecode('xyz/a%20b%20c%20.pdf');
This is known as URL Encoding. You need to decode the string. If you are using jQuery you should check out the URL Encode plug in.
You need to urldecode (as stated above).
However, you say that you can remove the %20 and it will work. I would say you need them, they decode to spaces. Check it out using this online url decoder:
http://www.convertstring.com/EncodeDecode/UrlDecode
it decodes to:
xyz/a b c .pdf
not
xyz/abc.pdf