3

我想在我的 PHP 代码中删除我的段落中的几个问号。

例如:

$str = " Hi ?How? are you? Hope you are good. ";

输出应该是:

$str = " Hi How are you? Hope you are good. ";

所以段落中的单词有开始和结束字符“?” (问号),则应删除两个问号。请建议。请提出实现这一目标的最佳方法。

4

5 回答 5

0

试试这个:

$str = " Hi ?How? are you? Hope you are good. ";

$newstr = preg_replace('/\?([a-z0-9]{1,}?)\?/si','$1',$str);
echo $newstr;
于 2013-01-10T09:03:00.147 回答
0

尝试以下操作:

$str = preg_replace('/\\?(\\w+)\\?','$1',$str);

这将匹配由 . 包围的任何“单词” ?。匹配“\w任何单词字符(字母、数字、下划线)”。意思是“+一个或多个”。如果您还想匹配??和删除它们,可以将其替换为*.

于 2013-01-10T09:03:12.407 回答
0
$string = " Hi ?How? are you? Hope you are good. ";
$pattern = "/\?(\w+)\?/";
$replacement = '${1}';
echo preg_replace($pattern, $replacement, $string);
于 2013-01-10T09:03:30.020 回答
0
$str = " Hi ?How? are you? Hope you are good. ";
echo preg_replace("/\?(\w+)\?/", "$1", $str);
于 2013-01-10T09:04:53.827 回答
0

最简单的正则表达式将是\?([^\?\s]+)\?您的单词在第一组中的位置。你可能不得不像 php 需要它一样逃避它。

于 2013-01-10T09:05:12.770 回答