- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个相当基本的情况,我有一个字符串数组,我想在一个字符串中找到所有匹配项,并在它们周围放置强标签。这是我目前所拥有的:
$searchWords = array("test","this","s");
for($i=0;$i<sizeof($searchWords);$i++) {
$searchWords[$i] = "/".preg_quote($searchWords[$i])."/i";
}
$label = "This is a test string.";
$result = preg_replace($searchWords, "<strong>$0</strong>", $label);
echo($result);
问题是 preg_replace 函数似乎将“s”搜索词与被替换的强标签相匹配。所以我结束了:
<strong>Thisstrong> is a <strong>teststrong>.
当我真正想要的是:
<strong>This</strong> i<strong>s</strong> a <strong>test</strong>.
那么,你们能指出我哪里出错了吗?
非常感谢任何帮助,我为此焦头烂额,我一定很接近。
最佳答案
你不想做三个替换,但一个:
$result = preg_replace("#" . implode($searchWords, "|") . "#", "<strong>$0</strong>", $label);
hakre 的编辑:这就像这个或那个或那个字符串。将进行第一场比赛。所以从最长的字符串开始,把最小的字符串作为最后一个。
binary 对此编辑的回答:查看评论
完整版:
<?php
$searchWords = array("t", "test", "this");
usort($searchWords, function ($a, $b) { return strlen($b) - strlen($a); });
foreach ($searchWords as &$word)
{
$word = preg_quote($word);
}
unset($word);
$label = "This is a test string.";
$searchWords = implode($searchWords, "|");
$result = preg_replace("#{$searchWords}#i", "<strong>$0</strong>", $label);
echo($result);
关于PHP preg_replace 函数替换以前的匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11361943/
有人好心地帮我解决了一个非常具体的用途的正则表达式,但我现在唯一的问题是正则表达式只替换了匹配的一个实例,而不是所有匹配(这是目标)。 我想要 的每个实例在此处找到“stylish-blue-but
PHP正则表达式替换实现是如何的呢?首先向你介绍下PHP preg_replace,PHP preg_replace的使用是我们实现的方法,那么对于PHP正则表达式替换实现过程我们从实例入手。 P
如何更改所有出现的 在文本文件的每一行中(每行总共有 5 个 )使用 preg_replace (用 | 代替)? 我的代码是: preg_replace("//", "|", $text); 最佳答
如何更改所有出现的 在文本文件的每一行中(每行总共有 5 个 )使用 preg_replace (用 | 代替)? 我的代码是: preg_replace("//", "|", $text); 最佳答
C#中的PHP preg_replace是什么? 我有一个字符串数组,我想用另一个字符串数组替换它。这是 PHP 中的示例。我如何在不使用 .Replace("old","new") 的情况下在 C#
如果有一件事我无法理解(或学习),那就是 preg_replace句法。我需要帮助删除 > 之间所有可能的符号(空格、制表符、换行符等)和 BANK ME 773264 17072012
preg_replace("/{{(.*?)}}/e","$$1",$rcontent); 请向我解释一下声明...我无法理解 最佳答案 考虑一个使用示例: $rcontent = "abc {{fo
我使用 netbeans,我尝试用 \\ 替换 \ 但它失败了,它无法转义 \\ 字符。 这不是 Netbeans 问题,而是 PHP 问题。 preg_replace('\','\\','text
给定如下字符串 $text= 'You must call [[abc\base\Object:: canGetProperty()|canGetProperty()]] or
非常简单的问题:如何preg_replace 反斜杠字符? 最佳答案 是的,但你需要逃避它。在正则表达式中使用它时,使用 \\ 在替换中使用它,使用 \\\\ (这将变成 \\将被解释为单个反斜杠)。
我正在使用一个 CMS 系统,该系统坚持在 之间放置大量垃圾标记和空标签。和 标签。 我正在尝试使用正则表达式来匹配和删除这些垃圾(遗憾的是修复 CMS 是不可能的)。 我似乎创建了一个有点太饿的正
我注意到替换的是 \那么该功能将无法按预期工作。所以我应该转义反斜杠。 还有哪些字符应该被转义?我没有成功找到任何文档。 我不能使用 preg_quote()因为它用于转义模式而不是替换。 再次编辑:
我正在尝试使用正则表达式从标签中提取信息,然后根据标签的各个部分返回结果。 preg_replace('//', analyze(array($0, $1, $2)), $src); 所以我正在抓取零
我想替换 html 字符串中给定的电话号码,例如 click here now! (123) -456-789 我认为最好的方法是找到所有看起来像电话号码的不同情况,例如: $pattern = *a
我很难尝试突出显示格式化电话号码的搜索结果。 $search_txt='5678'; // user generated (can be anything) $phone_number='(123)
我正在尝试使用 php 和 preg_replace 从 yell.com 中提取邮政编码。我成功提取了邮政编码,但只提取了地址。这是一个例子 $URL = "http://www.yell.com/
我想删除字符串上的所有 html 标签(包括它们的属性,如 class、src、id 等),但我想保留 , 和 标签。我将如何实现这一点? 如有任何建议,我们将不胜感激。 谢谢! 最佳答案 使用st
我正在努力完成以下任务: 搜索小写字母后跟大写字母。将其替换为小写字母,后跟 '. ',后跟大写字母。 例子: helloAre you there 应该变成: hello. Are you ther
好吧,我可能不会以正确的方式解决这个问题,但这里是.. 我有这个字符串,它接受一个链接并提取标签之间的文本... $string = $item; $pattern = '/\]*)\>([^/i';
我想用PHP替换一个词preg_replace .如果该词之前存在字符 />,则不应替换该词。 即 如果匹配就应该替换它 $word = "foo"; 如果匹配则不应替换 $word = "/>foo
我是一名优秀的程序员,十分优秀!