[英]Search and Replace with Regex
I am trying to search through text for a specific word and then add a html tag around that word.For example if i had the string "I went to the shop to buy apples and oranges" and wanted to add html bold tags around apples. 我试图通过文本搜索特定的单词,然后在该单词周围添加一个html标签。例如,如果我有字符串“我去商店买苹果和橘子”,并希望在苹果周围添加html粗体标签。
The problem, the word i search the string with is stored in a text file and can be uppercase,lowercase etc.When i use preg_replace to do this i manage to replace it correctly adding the tags but for example if i searched for APPLES and the string contained "apples" it would change the formatting from apples to APPLES, i want the format to stay the same. 问题,我搜索字符串的单词存储在一个文本文件中,可以是大写,小写等。当我使用preg_replace这样做时,我设法正确地替换它添加标签但是例如,如果我搜索APPLES和字符串包含“苹果”它会改变格式从苹果到APPLES,我希望格式保持不变。
I have tried using preg_replace but i cant find a way to keep the same word casing.This is what i have: 我尝试过使用preg_replace但我找不到保持相同单词的方法。这就是我所拥有的:
foreach($keywords as $value)
{
$pattern = "/\b$value\b/i";
$replacement = "<b>$value</b>";
$new_string = preg_replace($pattern, $replacement, $string);
}
So again if $value was APPLES it would change every case format of apples in the $string to uppercase due to $replacemant having $value in it which is "APPLES". 因此,如果$ value为APPLES,它会将$ string中的每个case格式更改为大写,因为$ replacemant中的$ value为“APPLES”。
How could i achieve this with the case format staying the same and without having to do multiple loops with different versions of case format? 我怎样才能在案例格式保持不变的情况下实现这一点,而不必使用不同版本的案例格式进行多个循环?
Thanks 谢谢
Instead of using $value
verbatim in the replacement, you can use the literal strings \\0
or $0
. 您可以使用文字字符串
\\0
或$0
,而不是在替换中使用$value
逐字。 Just as \\n
/ $n
, for some integer n
, refers back to the n
th capturing group of parentheses, \\0
/ $0
is expanded to the entire match. 就像
\\n
/ $n
,对于某个整数n
,返回第n
个捕获括号组, \\0
/ $0
扩展到整个匹配。 Thus, you'd have 因此,你有
foreach ($keywords as $value) {
$new_string = preg_replace("/\\b$value\\b/i", '<b>$0</b>', $string);
}
Note that '<b>$0</b>'
uses single quotes. 请注意,
'<b>$0</b>'
使用单引号。 You can get away with double quotes here, because $0
isn't interpreted as a reference to a variable, but I think this is clearer. 你可以在这里使用双引号,因为
$0
不被解释为对变量的引用,但我认为这更清楚。 In general, you have to be careful with using a $
inside a double-quoted string, as you'll often get a reference to an existing variable unless you escape the $
as \\$
. 一般来说,你必须小心在双引号字符串中使用
$
,因为你经常会得到对现有变量的引用,除非你将$
转义为\\$
。 Similarly, you should escape the backslash in \\b
inside the double quotes for the pattern; 同样,你应该在模式的双引号内转义
\\b
的反斜杠; although it doesn't matter in this specific case, in general backslash is a meaningful character within double quotes. 虽然在这种特定情况下无关紧要,但通常反斜杠在双引号内是有意义的字符。
I might have misunderstood your question, but if what you are struggling on is differentiating between upper-case letter (APPLE) and lower-case letter (apple), then the first thing you could do is convert the word into upper-case, or lower-case, and then run the tests to find it and put HTML tags around it. 我可能误解了你的问题,但是如果你正在努力的是区分大写字母(APPLE)和小写字母(apple),那么你可以做的第一件事就是将单词转换为大写字母,或者小写,然后运行测试以找到它并在其周围放置HTML标记。 That is just my guess and maybe I completely misunderstood the question.
这只是我的猜测,也许我完全误解了这个问题。
In the code exists offtopic error: the result value have been rewritten on not first loop iteration. 在代码中存在offtopic错误:结果值已经被重写,而不是第一次循环迭代。 And ending value of
$new_string
will be only last replacement. $new_string
结束值将只是最后一次替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.