PHP:插入文本直到定界符 [英] PHP: insert text up to delimiter

查看:74
本文介绍了PHP:插入文本直到定界符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆这样的聊天记录:

I have a bunch of chat logs that look like this:

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

我只想突出显示名称.我写了一些应该做的代码,但是,我想知道是否有比这更干净的方法:

I want to highlight the just the names. I wrote some code that should do it, however, I was wondering if there was a much cleaner way than this:

$line= "name: text";
$newtext = explode(":", $line,1);
$newertext = "<font color=red>".$newtext[0]."</font>:";
$complete = $newertext.$newtext[1];
echo $complete;

推荐答案

看起来不错,尽管您可以保存temp变量:

Looks fine, although you can save the temp variables:

$newtext = explode(":", $line,1);
echo "<font color=red>$newtext[0]</font>:$newtext[1]";

这可能会更快,也可能不会,您必须进行测试:

This might be faster or might not, you'd have to test:

echo '<font color=red>' . substr_replace($line, '</font>', strpos($line, ':') , 0);

这篇关于PHP:插入文本直到定界符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆