Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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_Arrays_Regex_Preg Replace - Fatal编程技术网

PHP在数组元素之前和之后添加文本

PHP在数组元素之前和之后添加文本,php,arrays,regex,preg-replace,Php,Arrays,Regex,Preg Replace,如何将标记环绕这是第一行,这是第二行,这是第三行在下面的$thisStr中,因此结果是: `<i>This is line one!</i>` `<i>This is line two!</i>` `<i>This is line three!</i>` `这是第一行` `这是第二行` `这是第三行` 我所做的只是在::部分周围放置标签。我无法让它来回移动并包装标签 $thisStr='这是第一行!::这是第二行!::这

如何将
标记环绕
这是第一行
这是第二行
这是第三行在下面的
$thisStr
中,因此结果是:

`<i>This is line one!</i>`
`<i>This is line two!</i>`
`<i>This is line three!</i>`
`这是第一行`
`这是第二行`
`这是第三行`
我所做的只是在
::
部分周围放置标签。我无法让它来回移动并包装标签

$thisStr='这是第一行!::这是第二行!::这是第三行


echo preg_替换(“/(:)/i“,“$1”,$thisStr)

这非常有效,但不是预替换:

$thisStr = 'This is line one! :: This is line two! :: This is line three!';

$result= "<i>".str_replace("::","</i><i>",$thisStr)."</i>";

echo $result;
$thisStr='这是第一行!::这是第二行!::这是第三行;
$result=”“.str_replace(“::”,“,$thisStr”);
回声$结果;
或者,为了与您的确切示例相匹配:

$thisStr = 'This is line one! :: This is line two! :: This is line three!';

$result= "'<i>".str_replace(" :: ","</i>'<br/>'<i>",$thisStr)."</i>'";

echo $result;

/*
'This is line one!'
'This is line two!'
'This is line three!'
*/
$thisStr='这是第一行!::这是第二行!::这是第三行;
$result=“”.str_replace(“::”,“
”,$thisStr)。“; 回声$结果; /* “这是一号线!” “这是第二行!” “这是第三行!” */
试试这个:

$thisStr = 'This is line one! :: This is line two! :: This is line three!';
$lines = explode('::', $thisStr);
$result = '';
foreach($lines as $line) {
  $result .= '<i>' . $line .'</i>';
}
echo htmlentities($result);
$thisStr='这是第一行!::这是第二行!::这是第三行;
$lines=explode(“::”,$thisStr);
$result='';
foreach($line作为$line){
$result.=''.$line';
}
回显率($结果);

希望这能有所帮助。

如果您真的需要使用preg\u,请更换它

$input_lines = "This is line one!
This is line two!
This is line three!";

$New_string = preg_replace("/This is line .*/", "<i>$0</i>", $input_lines);
$input\u lines=“这是第一行!
这是第二行!
这是第三行!”;
$New_string=preg_replace(“/这是行。*/”、“$0”、$input_行);
模式查找字符串“this is line”,而。*表示任何长度的任何内容。
那么替换模式就很容易理解了

但我推荐Verjas写的答案。

preg_replace添加的额外复杂性没有必要。

thisStr
最初来自哪里?如果可以插入
分隔符,也可以直接添加元素,对吗?另一方面,
元素是表示性标记,应该避免使用
。它来自数组的内爆。像这样
$lines[]='这是第一行!'$lines[]=“这是第二行!”$thisStr=内爆(“::”,$lines)
但是我需要
$thisStr
它的方式,然后再次使用
tagsUmm,以及什么阻止您只做
$italicized=''。内爆(“”,$lines)。“”?就像我说的,如果你可以插入
::
,你也可以插入任何其他内容…下面是我试图做的
preg\u replace(“/^(.*?)$/I”,“$1”,“$array”)。这是可行的,但我不确定它的效率有多高。而且,文本不是静态的。根据生成的错误消息,它可以是任何内容;点击preg_replace@Norman不要介意。。你写的东西我读得不够好。我相信他们同样高效,Varjas和preg_取代了他们。使用你认为最容易理解的方法