PHP:替换所有实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7297052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:25:18  来源:igfitidea点击:

PHP: Replace all instances

phpregexreplacepreg-replace

提问by lemon

I have a chatlog file that looks like this (name represents screenames and text is their chat string)

我有一个看起来像这样的聊天记录文件(名称代表屏幕名称,文本是他们的聊天字符串)

name: some text
name2: more text
name: text
name3: text

I want to color all names up to the :red.
For example: <font color=red>myname:</fontcolor> helloHow would I do this?

我想把所有的名字都涂成:红色。
例如:<font color=red>myname:</fontcolor> hello我该怎么做?

I'm not sure why, but this code colors everything afterthe colon

我不知道为什么,但是这段代码在冒号之后给所有东西上色

echo preg_replace('/(.*?):/', "<font color=#F00></font>:", $output);

回答by Bassem

A correct answer to this question has been provided previously:

之前已经提供了对这个问题的正确答案:

Look at the second answer:

看第二个回答:

PHP: insert text up to delimiter

PHP:插入文本到分隔符

In addition, your implementation is wrong, look at the regular expression it should start with ^ :

另外,你的实现是错误的,看看它应该以 ^ 开头的正则表达式:

echo preg_replace('/(.*?):/', "<font color=#F00></font>:", $output);

Should be:

应该:

echo preg_replace('/^(.*?):/', "<font color=#F00></font>:", $output);

回答by Hosh Sadiq

try:

尝试:

echo preg_replace('/^(.*?):(.*?)$/s', "<font color=#F00>\1</font>:\2", $output);

EDIT: This should work (tried it):

编辑:这应该有效(尝试过):

trim(preg_replace("/(?:\n)(.*?):(.*?)/s", "<font color=#F00>\1</font>:\2", "\n".$str))

Final try, maybe try to explode it instead:

最后的尝试,也许尝试爆炸它:

<?php
$content = 'name: some text
name2: more text
name: text
name3: text';
$tmp = explode("\n", $content);
for($i = 0; $i < count($tmp); $i ++) {
    $tmp[$i] = '<span style="color:#F00">'.str_replace(':', '</span>:', $tmp[$i], 1);
}
echo implode("\n", $tmp);
?>

This does assume that whatever is before the colon, it won't have another colon.

这确实假设冒号之前的任何内容都不会有另一个冒号。



My bad, I misunderstood str_replace()'s last parameter. Try this:

糟糕,我误解了 str_replace() 的最后一个参数。尝试这个:

<?php
$tmp = explode("\n", $content);
for($i = 0; $i < count($tmp); $i ++) {
    $tmp2 = explode(':', $tmp[$i]);
    $tmp2[0] = '<span style="color:#F00">'.$tmp2[0].'</span>';
    $tmp[$i] = implode(':', $tmp2);
}
echo implode("\n", $tmp);

回答by steve

put the : inside the font tag after $1

将 : 放在 $1 之后的字体标签中

echo preg_replace('/^(.*?):/', "<font color=#F00>:</font>", $output);

回答by Mr Coder

Try this

尝试这个

echo preg_replace('/([a-zA-Z0-9]*):/', "<font color=#F00></font>:", $output);

回答by mario

Make the regex more specific:

使正则表达式更具体:

= preg_replace('/^(\w+):/m', ...

Or if usernames can contain non-alphanum symbols:

或者,如果用户名可以包含非字母数字符号:

= preg_replace('/^(\S+):/m', "<b></b>:", $output);