gpt4 book ai didi

php - 如何从字符串中删除单引号和双引号

转载 作者:可可西里 更新时间:2023-10-31 22:52:45 25 4
gpt4 key购买 nike

当我通过此函数运行包含双引号的短语时,它会将引号替换为引号。

我想完全删除它们(也包括单引号)。我怎样才能改变函数来做到这一点?

function string_sanitize($s) {
$result = preg_replace("/[^a-zA-Z0-9]+/", "", $s);
return $result;
}

更新:

Example 1: This is 'the' first example 
returns: Thisis030the039firstexample
Errors: Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '0' in C


Example 2: This is my "second" example
returns: Thisismyquotsecondquotexample
Errors: Invalid express in Xpath

最佳答案

我不会调用该函数 string_sanitize(),因为它具有误导性。您可以将其称为 strip_non_alphanumeric()

您当前的函数将去除任何不是大写或小写字母或数字的内容。

你可以只删除 '" ......

$str = str_replace(array('\'', '"'), '', $str); 

关于php - 如何从字符串中删除单引号和双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5454273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com