Loko
Loko

Reputation: 195

special character and emoji from ios to php

I've looked at a lot of threads, but i didn't find the correct solution or it didn't work... It makes me angry^^.

I have tried so many things but none of them worked properly.

My problem is very easy .. i think ;)

I want send a text from my iphone to a php script and then save it in a SQL database.

First, the special characters do not work. A " will be &\quot; (without \ :O ) and so on. Next the emoji from iphone to php change to e.g. ðŸ˜.

Here is my iPhone Code to send a text to php:

NSString *post = [[NSString alloc] initWithFormat:@"&action=messageAdd&text=%@"textField.text];
    NSData * postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
    NSString * postLength = [NSString stringWithFormat:@"%d",[postData length]];
    NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://xxxx.com/xx.php"]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

And here the Code from the PHP-file

header('Content-Type: text/html; charset=utf-8');
..
..
..
$text = htmlentities(trim(stripinput($_POST['text'])));

This brings me all a bit confused with all the different variants. I hope someone can steer me in the right direction. Codes are always welcome :)

Upvotes: 1

Views: 4333

Answers (4)

Rio Bautista
Rio Bautista

Reputation: 443

I have a similar problem, and I resolved the MSSQL store part without altering the database by storing a base64 encoded string of the text using:

+(NSString*) base64EncodeString:(NSString*)string {
    return [@"b64:" stringByAppendingString:[[string dataUsingEncoding:NSUTF32BigEndianStringEncoding] base64EncodedStringWithOptions:0]];
}

+(NSString*) base64DecodeString:(NSString*)string {
    if ([string hasPrefix:@"b64:"]) {
        string = [string substringFromIndex:4];
        string = [[NSString alloc] initWithData:[[NSData alloc] initWithBase64EncodedString:string options:0] encoding:NSUTF32BigEndianStringEncoding];
    }
    return string;
}

Notice I prefixed my string with b64: so my code will still work with non-encoded text.

It worked as I wanted to, I can now save emojis and multi-byte characters on my MS-SQL server and retrieve them from my IOS devices without bothering about my database collation.

This also resolves the single-quote issues as well.

BUT! I'm also stuck with the same problem as you are. I wanted to get my PHP site to display the emojis and MB characters.

Upvotes: 0

Matteo Gobbi
Matteo Gobbi

Reputation: 17707

when you send the data string with emoji to your php page, you have to use this encoding:

NSData *data = [string dataUsingEncoding:NSNonLossyASCIIStringEncoding];

if you have to send a string of this data you can convert it in a string with:

NSString str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

so, you'll have a string like to \ucode instead of the smile.

When you receive the string from database to app, you can do:

NSString str = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];

And the fame is done. ;)

Upvotes: 5

Loko
Loko

Reputation: 195

i have finally solved the problem! but there is now a new problem..

i changed the database to utf8mb4 and after the connection to the database i call

mysql_query("SET NAMES 'utf8mb4'");

In the database the emojis are displayed as

?

but on ios device it is displayed as emoji.

the question now is how can i display the "?" from the mysql database to my website?

Upvotes: -1

Jeffrey Blake
Jeffrey Blake

Reputation: 9709

Regarding the Emoji:

Chances are that the issue with the emoji comes from an encoding difference (i.e. your php page is not displaying with the same text encoding as the iphone is using when it sends the text).

Regarding the Quotes:

When you call htmlentities(), you are specifically converting characters which have an html equivilant into that html equivilant. That means that " will be converted to ".

You can use the ENT_NOQUOTES flag to cause this function to leave single and double quotes unconverted:

$text = htmlentities(trim(stripinput($_POST['text'])), ENT_NOQUOTES);

Note that this can be dangerous if the quotes will impact what you're doing elsewhere with the text. Strings output to the page could render strangely if the quotes alter how the html fits, and database interactions could be impacted if you do not properly account for quotes in your database-related code.

Upvotes: 0

Related Questions