使用 PHP preg_replace 函数进行复杂模式替换,忽略带引号的字符串

发布于 2024-09-01 09:21:39 字数 1017 浏览 3 评论 0原文

考虑以下字符串:

这是一个 STRING,其中包含一些关键字 有可用的。 '我需要格式化 来自 STRING' 的关键字

在上面的字符串关键字中,有 STRINGWHERE

现在我需要得到如下输出:

this is a <b>STRING</b> <b>WHERE</b> some keywords ARE available. 'i need TO format the KEYWORDS from the STRING'

这样html 输出将类似于:

这是STRING WHERE一些关键字 有可用的。 '我需要格式化 来自 STRING' 的关键字

请注意,带引号 ('...') 字符串中的关键字将被忽略。在上面的示例中,我忽略了带引号的字符串中的 STRING 关键字。

请提供以下 PHP 脚本的修改版本,以便我可以获得如上所述的所需结果:

$patterns = array('/STRING/','/WHERE/');
$replaces = array('<b>STRING</b>', '<b>WHERE</b>');
$string   = "this is a STRING WHERE some keywords ARE available. 'i need TO format the KEYWORDS from the STRING'";
preg_replace($patterns, $replaces, $string);

Consider the following string:

this is a STRING WHERE some keywords
ARE available. 'i need TO format the
KEYWORDS from the STRING'

In the above string keywords are STRING and WHERE

Now i need to get an output as follows:

this is a <b>STRING</b> <b>WHERE</b> some keywords ARE available. 'i need TO format the KEYWORDS from the STRING'

So that the html output will be like:

this is a STRING WHERE some keywords
ARE available. 'i need TO format the
KEYWORDS from the STRING'

Note that the keywords within a quoted ('...') string will be ignored. in the above example i ignored the STRING keyword within the quoted string.

Please give a modified version of the following PHP script so that I can have my desired result as above :

$patterns = array('/STRING/','/WHERE/');
$replaces = array('<b>STRING</b>', '<b>WHERE</b>');
$string   = "this is a STRING WHERE some keywords ARE available. 'i need TO format the KEYWORDS from the STRING'";
preg_replace($patterns, $replaces, $string);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒几许 2024-09-08 09:21:39

这适用于您的字符串示例,但对于更复杂的字符串会出现问题,例如包含带有撇号的单词的字符串。无论如何,它可以作为一个起点。

$keywords = array("STRING", "WHERE");
$regexp = '/(\'[^\']+\')|\b(' . implode('|', $keywords) . ')\b/e';
preg_replace($regexp, "strlen('\\2') ? '<b>\\2</b>' : '\\0'", $string);

This will work with your string example, but there will be problems with more complicated strings, for example those containing words with apostrophes. Anyway, it may be used as a starting point.

$keywords = array("STRING", "WHERE");
$regexp = '/(\'[^\']+\')|\b(' . implode('|', $keywords) . ')\b/e';
preg_replace($regexp, "strlen('\\2') ? '<b>\\2</b>' : '\\0'", $string);
时光病人 2024-09-08 09:21:39

尝试类似的操作:

$keywords = array(
  'STRING' ,
  'WHERE' ,
  'KEYWORDS'
);
$keywordsRE = array();
foreach( $keywords as $w ) {
  $keywordsRE[] = '/\b('.$w.')\b/';
}
$string = "this is a STRING WHERE some keywords KEYWORDS ARE available. 'i need TO format the KEYWORDS from the STRING'";
$stringParts = explode( "'" , $string );
foreach( $stringParts as $k => $v ) {
  if( !( $k%2 ) )
    $stringParts[$k] = preg_replace( $keywordsRE , '<b>$1</b>' , $v );
}
$stringReplaced = implode( "'" , $stringParts );

重复相同的关键字(具有相同的更改)有点多余 - 使用正则表达式允许您应用这些相同的更改(在这种情况下,将匹配项包装在 ; 标签)到所有匹配项。

Try something like:

$keywords = array(
  'STRING' ,
  'WHERE' ,
  'KEYWORDS'
);
$keywordsRE = array();
foreach( $keywords as $w ) {
  $keywordsRE[] = '/\b('.$w.')\b/';
}
$string = "this is a STRING WHERE some keywords KEYWORDS ARE available. 'i need TO format the KEYWORDS from the STRING'";
$stringParts = explode( "'" , $string );
foreach( $stringParts as $k => $v ) {
  if( !( $k%2 ) )
    $stringParts[$k] = preg_replace( $keywordsRE , '<b>$1</b>' , $v );
}
$stringReplaced = implode( "'" , $stringParts );

Repeating the same keywords (with the same changes) is a bit redundant - using a Regular Expression allows you to apply those same changes (in this case, wrapping the matches in <b></b> tags) to all matches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文