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_取代了他们。使用你认为最容易理解的方法