Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

php

mysql

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('[email protected]','',$data,'From: [email protected]')){
        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 “ ” ‘ ’
like image 510
drooh Avatar asked Dec 23 '17 00:12

drooh


1 Answers

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.

like image 175
Jeremy Jones Avatar answered Sep 30 '22 04:09

Jeremy Jones