Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中仅从字符串中删除特定的特殊字符?_Php_String - Fatal编程技术网

如何在PHP中仅从字符串中删除特定的特殊字符?

如何在PHP中仅从字符串中删除特定的特殊字符?,php,string,Php,String,我有一根像下面这样的线 《印度斯坦时报》,2009年10月,a 著名艺术评论家谈她的独唱 贾瓦哈尔卡拉斋浦尔展览 肯德拉,2009年9月23日至29日。“许多人 她的画包括她自己 人像,强调人文 独特的困境和漫无目的” 在上面的字符串中,我需要删除以下字符 $str = "Hindustan Times, Oct 2009, Review by a well known Art critic on her solo exhibition at Jaipur, Jawahar Kala Kendr

我有一根像下面这样的线

《印度斯坦时报》,2009年10月,a 著名艺术评论家谈她的独唱 贾瓦哈尔卡拉斋浦尔展览 肯德拉,2009年9月23日至29日。“许多人 她的画包括她自己 人像,强调人文 独特的困境和漫无目的”

在上面的字符串中,我需要删除以下字符

$str = "Hindustan Times, Oct 2009, Review by a well known Art critic on her solo exhibition at Jaipur, Jawahar Kala Kendra'th, 23-29th Sep 2009. \"Many of her paintings including her self portrait, stress in humanities singular plight and aimlessness";
$search     = array(',', '"', "'", '-', '.');
$clean      = str_replace($search, ' ', $str); 
echo $clean; 
-


有什么字符串函数可以用来删除这些字符吗?

使用preg\u replace,并用空字符串替换所需的集。

。经验法则基本上是只在其他字符串方法无法(或至少不能很好地)的情况下使用正则表达式

但是,如果您想使用正则表达式,也可以这样做

您可以在字符类中输入这些字符,注意转义字符串分隔符,并使用
-
的方式将其按字面理解,而不是作为范围

preg_replace('/[,"\'.-]+/', '', $str);
.

您可以使用替换字符数组

$str = "Hindustan Times, Oct 2009, Review by a well known Art critic on her solo exhibition at Jaipur, Jawahar Kala Kendra'th, 23-29th Sep 2009. \"Many of her paintings including her self portrait, stress in humanities singular plight and aimlessness";
$search     = array(',', '"', "'", '-', '.');
$clean      = str_replace($search, ' ', $str); 
echo $clean; 

或者,您可以选择删除所有非字母数字或空格的字符,而不是列出所有不需要的字符:

preg_replace("/[^A-Za-z0-9\s]/", "", $str);

当然,这会去掉所有的标点符号,可能还有比你想要的更多的字符。

这将选择任何字符,
将匹配任何字符。@Nightfirecat不在字符类中。这有助于我学习正则表达式^_^