Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.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 preg_replace()包含一个字符串*&引用;性格_Php_Preg Replace - Fatal编程技术网

Php preg_replace()包含一个字符串*&引用;性格

Php preg_replace()包含一个字符串*&引用;性格,php,preg-replace,Php,Preg Replace,我制作了一个脚本来突出显示字符串中的一个单词。脚本如下 function highlight_text($text, $words){ $split_words = explode( " " , $words ); foreach ($split_words as $word){ $color = '#FFFF00'; $text = preg_replace("|($word)|Ui", "<span style=\"background:".$color.";\

我制作了一个脚本来突出显示字符串中的一个单词。脚本如下

function highlight_text($text, $words){
  $split_words = explode( " " , $words );
  foreach ($split_words as $word){
    $color = '#FFFF00';
    $text = preg_replace("|($word)|Ui", "<span style=\"background:".$color.";\">$1</span>", $text );
  }
  return $text;
}

$text = '*bc';
$words = '*';

echo highlight_text($text, $words);

有人能帮我吗?

“*”
更改为
“\*”
并获利。

您可以检查函数中的特殊字符是否突出显示\u文本

比如:

函数突出显示文本($text,$words){
$split_words=分解(“,$words);
foreach($word拆分为$word){
$str='';
$word=str_split($word);
foreach($c){
如果($c=='*')){
$str.='\*';
}
否则{
$str.=$c;
}
}
$color='#FFFF00';
$text=preg_replace(“|($str)| Ui”、“$1”、$text);
}
返回$text;
}
将代码更改为

function highlight_text($text, $words){
    $split_words = explode( " " , $words );
    foreach ($split_words as $word){
        $color = '#FFFF00';
        $word = preg_quote($word, '/');
        $text = preg_replace("|$word|Ui", "<span style=\"background:".$color.";\">$0</span>", $text );
    }
    return $text;
}

$text = '*bc';
$words = '*';

echo highlight_text($text, $words);
函数突出显示文本($text,$words){
$split_words=分解(“,$words);
foreach($word拆分为$word){
$color='#FFFF00';
$word=preg_quote($word,“/”);
$text=preg_replace(“|$word | Ui”、“$0”、$text);
}
返回$text;
}
$text='*bc';
$words='*';
回显突出显示文本($text,$words);
然后就可以了。

这可以帮助您:

<?php
 function highlight_text($text, $words){
  $split_words = explode( " " , $words );
  foreach ($split_words as $key=>$word){
  if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $word))// looking for special characters
                 {
                    $word = preg_quote($word, '/');// if found output \ before that 
                          }
    $color = '#FFFF00';
    $text = preg_replace("|($word)|Ui", "<span style=\"background:".$color.";\">$1</span>", $text );
  }
  return $text;
}

$text = '*bc';
$words = '*';

echo highlight_text($text, $words);

尝试使用preg_quote()转义“*”或单词,那么
word?
word+word
呢?或者word+word…?是的,我的代码有一个bug。一楼是对的。谢谢。word呢?还是单词+单词。。。?
function highlight_text($text, $words){
    $split_words = explode( " " , $words );
    foreach ($split_words as $word){
        $color = '#FFFF00';
        $word = preg_quote($word, '/');
        $text = preg_replace("|$word|Ui", "<span style=\"background:".$color.";\">$0</span>", $text );
    }
    return $text;
}

$text = '*bc';
$words = '*';

echo highlight_text($text, $words);
<?php
 function highlight_text($text, $words){
  $split_words = explode( " " , $words );
  foreach ($split_words as $key=>$word){
  if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $word))// looking for special characters
                 {
                    $word = preg_quote($word, '/');// if found output \ before that 
                          }
    $color = '#FFFF00';
    $text = preg_replace("|($word)|Ui", "<span style=\"background:".$color.";\">$1</span>", $text );
  }
  return $text;
}

$text = '*bc';
$words = '*';

echo highlight_text($text, $words);