Monday, January 22, 2018

How to use php to convert odd characters to single characters from iPhone input

Leave a Comment

So I have a form that when submitted by iPhone, if the user enters ’ it is entered in the database at ’.

I'm wondering if there is a way to convert this to a single character before entering it into the database. The main reason I need this is because it is being sent out as a txt message and every character counts.

I'd like to know if there is a function to convert these characters

— enters as — convert to - – enters as — convert to - “ enters as “ convert to " ” enters as †convert to " ‘ enters as ‘ convert to ' ’ enters as ’ convert to ' 

The problem really is not that it's stored that way in the database, but rather when the txt message is sent pulling data from the database.

In further testing, eliminating the database, did a test with a form submitting to php and emailing to sms gateway, when using phone to enter characters such as “ ” do not go through in the txt message, so this make me think that they are becoming mojibake. I have set in the page with the form.

Here is another illustration that demonstrates the problem. Here an iPhone (6s iO2 11.2.2 safari) submitting text to a php script which emails to an sms gateway, the text comes through without the special characters (“ ” ‘ ’), instead those characters are shown with a b, example text sent as “test” ‘test’ will come through in the txt as btestb btestb. Below is the ultra simple code that reproduces this issue.

filename: sms.php (using php 7.1.13)

<? if(isset($_POST['sub'])){     $data = isset($_POST['data'])?$_POST['data']:NULL;     if($data){         if(mail('5555555555@messaging.sprintpcs.com','',$data,'From: name@somedomain.com')){         echo 'sent!';         };     } } ?> <!DOCTYPE html> <html lang="en"> <head>     <title>test</title>     <meta charset="UTF-8" />     <meta content="minimum-scale=1.0, width=device-width, maximum-scale=1, user-scalable=no" name="viewport"> </head> <body>     <form action="sms.php" method="post" />         <input label="enter txt here" value="" name="data" />         <input type="submit" value="go" name="sub" />     </form> </body> </html> 
  • use an iPhone to enter the following characters “ ” ‘ ’

2 Answers

Answers 1

Essentially everything needs to be UTF-8 to deal with this. Tracking down the place where the corruption is happening is tedious but it's the only real answer. It could be early on, e.g. when the information is coming into the PHP script or going into the database, or later when it's being retrieved.

Final possibility to be kept in mind is that it might not really be corrupted at all -- sometimes it's just that the terminal or other output isn't set correctly (i.e. the very end of the chain), and it's just in checking it that it looks wrong due to your viewer, rather than the data itself or how it's being stored.

Answers 2

I reopened because this question implies that the Mojibake came from MySQL; the other question treated it as a PHP problem. PHP and HTML are unlikely to cause the problem; the source of the problem is mismatch of latin1 and utf8 when inserting/retrieving data via MySQL.

See "Mojibake" in Trouble with UTF-8 characters; what I see is not what I stored and ways to fix the data: http://mysql.rjweb.org/doc.php/charcoll#fixes_for_various_cases .

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment