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

Nor*_*man -2 php regex arrays preg-replace

我如何将<i> </i>标签包裹在下面的This is line one!, This is line two!,周围,结果是:This is line three!$thisStr

`<i>This is line one!</i>`
`<i>This is line two!</i>`
`<i>This is line three!</i>`
Run Code Online (Sandbox Code Playgroud)

我所做的只是在::零件周围贴上标签。我无法让它后退和前进并包裹标签。

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

echo preg_replace("/(::)/i", "<i>$1</i>", $thisStr);

ver*_*jas 5

这很好用,但不是 preg_replace:

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

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

echo $result;
Run Code Online (Sandbox Code Playgroud)

或者,为了匹配您的确切示例:

$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!'
*/
Run Code Online (Sandbox Code Playgroud)