0

I have a string of text:

$myString = "Lorem ipsum dolor sit ||||0101|||| amet, consectetur adipiscing elit. Morbi eu nisl sapien. Suspendisse quam mauris, eleifend in velit eget, molestie posuere urna. Nunc a dolor semper, sollicitudin orci et, suscipit arcu. Suspendisse ||||0103|||| augue erat, accumsan nec tellus vel, hendrerit ||||0102|||| tincidunt quam. Proin sit amet nunc non dolor pretium bibendum. Nunc aliquam, turpis iaculis ornare fermentum, felis ||||0104|||| neque lobortis neque";

I need to search through this text and replace all instances of ||||0101|||| with <img src="0101.jpg"/> & ||||0102|||| with <img src="0102.jpg"/> etc.

I can get all the instances of ids between the pipes using the below:

for ($i=0; $i<=10; $i++)
{
     if($i % 2) {
         $myStringPartsArray = explode("||||", $myString);
         $answer = $myStringPartsArray[$i];
         echo $answer."<br>";
     }    
}

but I am struggling to replace each instance with an image tag that uses the same id. Any suggestions would be greatly appreciated. Thanks.

4

1 回答 1

2
$myString = preg_replace('`\|\|\|\|(\d+)\|\|\|\|`', '<img src="$1.jpg"/>', $myString);
于 2013-10-17T20:35:35.610 回答