使用php从字符串中删除撇号

Xyl*_*lon 5 php

无论如何,要从php的字符串中删除撇号?示例:-如果字符串是Mc'win,则应显示为Mcwin

$Str = "Mc'win";
/*
    some code to remove the apostrophe
*/

echo $Str; // should display Mcwin
Run Code Online (Sandbox Code Playgroud)

小智 6

如果您的变量已被清理,您可能会沮丧地发现无法使用 $string = str_replace("'","",$string); 删除撇号;

$string = "A'bcde";
$string = filter_var($string, FILTER_SANITIZE_STRING);

echo $string." = string after sanitizing (looks the same as origin)<br>";
$string = str_replace("'","",$string);
echo $string." ... that's odd, still has the apostrophe!<br>";
Run Code Online (Sandbox Code Playgroud)

这是因为 sanitizing 将撇号转换为&#39;,但您可能不会注意到这一点,因为如果您回显字符串,它看起来与原始字符串相同。

您需要修改您的替换搜索字符,&#39; 以便在清理后起作用。

$string = str_replace("&#39;","",$string);
Run Code Online (Sandbox Code Playgroud)


Dev*_*von 5

您可以使用str_replace。

$Str = str_replace('\'', '', $Str);
Run Code Online (Sandbox Code Playgroud)

要么

$Str = str_replace("'", '', $Str);
Run Code Online (Sandbox Code Playgroud)

这将用代替所有的撇号(第2个参数)$Str。第一个示例转义了撇号,因此str_replace会将其识别为要替换的字符,而不是外壳的一部分。