Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:插入文本到分隔符_Php_String_Insert - Fatal编程技术网

PHP:插入文本到分隔符

PHP:插入文本到分隔符,php,string,insert,Php,String,Insert,我有一堆聊天日志,如下所示: name: some text name2: more text name: text name3: text $line = "name: text"; $complete = preg_replace('/^(name.*?):/', "<font color=red>$1</font>:", $line); echo $complete ; 我只想强调一下名字。我写了一些代码,但是,我想知道是否有比这更干净的方法: $line= "

我有一堆聊天日志,如下所示:

name: some text
name2: more text
name: text
name3: text
$line = "name: text";
$complete = preg_replace('/^(name.*?):/', "<font color=red>$1</font>:", $line);
echo $complete ;
我只想强调一下名字。我写了一些代码,但是,我想知道是否有比这更干净的方法:

$line= "name: text";
$newtext = explode(":", $line,1);
$newertext = "<font color=red>".$newtext[0]."</font>:";
$complete = $newertext.$newtext[1];
echo $complete;
$line=“名称:文本”;
$newtext=explode(“:”,$line,1);
$newertext=“”.$newtext[0]。“:”;
$complete=$newertext.$newtext[1];
echo$complete;

看起来不错,不过您可以保存临时变量:

$newtext = explode(":", $line,1);
echo "<font color=red>$newtext[0]</font>:$newtext[1]";
$newtext=explode(“:”,$line,1);
回显“$newtext[0]:$newtext[1]”;
这可能会更快,也可能不会,您必须测试:

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

也可以像这样尝试RegExp:

name: some text
name2: more text
name: text
name3: text
$line = "name: text";
$complete = preg_replace('/^(name.*?):/', "<font color=red>$1</font>:", $line);
echo $complete ;
$line=“名称:文本”;
$complete=preg_replace(“/^(名称。*?):/”,“$1:”,$line);
echo$complete;
编辑

如果它们的名称不是“name”或“name1”,只需在模式中删除名称,如下所示

$complete = preg_replace('/^(.*?):/', "<font color=red>$1</font>:", $line);
$complete=preg_replace(“/^(.*?:/”,“$1:”,$line);

gview发布的答案是它得到的最简单的答案,但是,作为引用,您可以使用正则表达式查找名称标记,并使用preg_replace()将其替换为新的html代码,如下所示:

// Regular expression pattern 
$pattern = '/^[a-z0-9]+:?/';

// Array contaning the lines
$str = array('name: some text : Other text and stuff',
        'name2: more text : : TEsting',
        'name: text testing',
        'name3: text Lorem ipsum');

// Looping through the array
foreach($str as $line)
{
    // \\0 references the first pattern match which is "name:" 
    echo preg_replace($pattern, "<font color=red>\\0</font>:", $line);
}
//正则表达式模式
$pattern='/^[a-z0-9]+:?/';
//包含线的数组
$str=array('name:some text:Other text and stuff',
'名称2:更多文本::测试',
'名称:文本测试',
“名称3:文本Lorem ipsum”);
//在数组中循环
foreach($str作为$line)
{
//\\0引用第一个模式匹配,即“名称:”
echo preg_替换($pattern,\\0:,$line);
}

hm I get
致命错误:只有变量可以通过引用传递
@kevin my bad。我和最后一个情人犯了个错误。str_replace的问题是,您不能将其约束为仅执行一次替换,因此这会导致会话中使用的“:”的其他出现错误。我用str_substr()更新了str_replace,这是一个选项,但由于这将是两个函数调用,我怀疑这可能比explode的效率低。我更改了程序的流程,因此不再逐行读取$line,而是读取并替换整个文件。这些解决方案只替换了第一行,但对原始问题非常有用+问题是,我不知道每个人的名字。它们的名称不是“name”或“name1”,请尝试使用此名称,而不是$complete=preg_replace(“/^(.*?:/”,“$1:”,$line);出于某种原因,这给结局涂上了颜色。name:text您可以使用preg_替换正确的正则表达式。然而,preg_u函数虽然非常强大,但要比explode或substr_ureplace慢得多。这就是为什么我没有提到它们。顺便说一句,这个标签已经被弃用了!:)使用。。。此外,请记住,属性必须始终引用!就像。@Qualcuno我不知道:P写起来要快得多,你是说?好吧,我们在2011年,这不是标准。你绝对应该避免使用它。@Qualcuno,否则我会冒险让未来的浏览器不支持它?我对此表示怀疑。但最好还是使用符合标准的代码。最终,这并不难!