替换preg_replace中的字符串

zah*_*in 2 php regex highlighting preg-replace

<?php
  $a="php.net s earch for in the all php.net sites this mirror only function 
      list online documentation bug database Site News Archive All Changelogs 
      just pear.php.net just pecl.php.net just talks.php.net general mailing 
      list developer mailing list documentation mailing list What is PHP? PHP 
      is a widely-used...";
?>
Run Code Online (Sandbox Code Playgroud)

我想强调具体的话.

例如php,netfunc:

php.net s earch for in the all **php**.**net** sites this mirror only **func**tion list online documentation bug database Site News Archive All Changelogs just pear.**php**.**net** just pecl.**php**.**net** just talks.php.net general mailing list developer mailing list documentation mailing list What is **PHP**? **PHP** is a widely-used...

谢谢你.

cod*_*ict 5

您可以执行以下操作:

// your string.
$str = "...............";

// list of keywords that need to be highlighted.
$keywords = array('php','net','fun');

// iterate through the list.
foreach($keywords as $keyword) {

    // replace keyword with **keyword**
    $str = preg_replace("/($keyword)/i","**$1**",$str);
}
Run Code Online (Sandbox Code Playgroud)

即使关键字是任何其他更大字符串的子字符串,上面也将替换关键字.要将关键字替换为完整字词,您可以执行以下操作:

$str = preg_replace("/\b($keyword)\b/i","**$1**",$str);
Run Code Online (Sandbox Code Playgroud)