问题描述
|
我有一个类似下面的字符串,
印度时报,2009年10月,由a撰写的评论
她的独奏著名艺术评论家
贾瓦哈尔卡拉斋浦尔展览
肯德拉,2009年9月23日至29日。
她的画作包括她的自我
肖像,人文压力
困境与漫无目的\“
在上面的字符串中,我需要删除以下字符
,
\"
\'
-
.
有什么我可以用来删除这些字符的字符串函数?
解决方法
另外,您可以选择去除所有不是字母数字或空格的字符,而不是列出所有不需要的字符:
preg_replace(\"/[^A-Za-z0-9\\s]/\",\"\",$str);
当然,这会删除所有标点符号,并且可能会删除更多的字符。
,您可以使用str_replace替换字符数组
$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,并用空字符串替换所需的集合。
,JohnP使用str_replace()
有正确的方法。经验法则基本上只使用正则表达式,而其他字符串方法则不会(或至少不是很好)。
但是,如果要使用正则表达式,也可以这样做。
您将在字符类中输入这些字符,请注意以转义字符串定界符,并以按字面意义而不是范围的方式使用-
。
preg_replace(\'/[,\"\\\'.-]+/\',\'\',$str);
伊迪恩