簡體   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