php stripslashes就像目标c中的函数一样

时间:2012-09-06 12:09:19

标签: ios objective-c iphone

在iphone应用程序中,我通过JSON将数据发送到Iphone设备。 在iphone objective c中是否有类似stripslashes的类似标签?

4 个答案:

答案 0 :(得分:0)

不,你必须手动完成。 E.g:

NSString *str = @"Test string with some \' and some \" \'\"";
str = [str stringByReplacingOccurrencesOfString:@"\'" withString:@"'"];
str = [str stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
NSLog(@"%@", str);

答案 1 :(得分:0)

要删除\中使用的简单O\'Reilly

string = [string stringByReplacingOccurrencesOfString:@"\\" withString:@""];

答案 2 :(得分:0)

一般striplashes(控制字符除外)

如果范围取消引用所有反斜杠引用的字符,例如stripslashes PHP函数而没有magic_quotes_sybase ,那么正常的表达式替换将会起作用,除了像NUL这样的控制字符({{ 1}}):

\0

反向添加

如果范围仅限于反转addslashes PHP函数,则只需要转义/取消引用4个字符:NUL,双引号,单引号和反斜杠。

NSString *str = @"\\\\ \\' \\\" \\r\\s\\t\\u";
str = [str stringByReplacingOccurrencesOfString:@"\\\\(.)" withString:@"$1" options:NSRegularExpressionSearch range:NSMakeRange(0, str.length)];

取消引用JSON

您根本不应将addslashesstripslashes用于JSON内容。 https://json.org/中描述了转义JSON的规则: enter image description here

例如,NSString *str = @"\\\\ \\' \\\" \\\0"; str = [str stringByReplacingOccurrencesOfString:@"\\\0" withString:@"\0"]; str = [str stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""]; str = [str stringByReplacingOccurrencesOfString:@"\\\'" withString:@"\'"]; str = [str stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"]; 不支持stripslashes + 4个十六进制数字

因此,如果范围是取消引用JSON格式(不是\u - ),那么使用JSON解析器:

stripslashes

答案 3 :(得分:-1)

请尝试这个

  NSString *valorTextField = [[NSString alloc] initWithFormat:@"Hello \ world"];
  NSString *replaced = [valorTextField stringByReplacingOccurrencesOfString:@"\" withString:@""];
 NSLog(@"%@", replaced);