繁体   English   中英

preg_grep不显示双引号(“)的结果

[英]preg_grep does not show result with double quotes(")

我的代码如下:

 $escapedOperator = ":";
 $operator['symbol'] = ":";
 $string = 'title: "space before" and text breaks';
 if(count(preg_grep('/\w["]*\s*'.$escapedOperator.'\s*["]*\w/',$string))){
       $search = "/\s*".$escapedOperator."\s*/";
       $string = preg_replace($search,$operator['symbol'],$string); 
 }else{
       $string=str_replace($operator['symbol'],"",$string);                 
 }

我正在输出:

title "space before" and text breaks 

但是我需要:

title:"space before" and text breaks 

如上所述, preg_grep()需要一个数组作为第二个参数,而不是字符串。 如果您更改:

$string = 'title: "space before" and text breaks';

至:

$string = array('title: "space before" and text breaks');

您的代码有效,并echo $string[0]; ,将输出title:"space before" and text breaks

如果目标是删除冒号(:)周围的空格,那么您是否可以这样做?

$string = 'title: "space before" and text breaks';
$string = preg_replace('/\s*:\s*/', ":", $string);
echo $string[0];

这还将输出title:"space before" and text breaks

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM