开发者

How to ignore first match in this regex

开发者 https://www.devze.com 2023-04-05 09:36 出处:网络
$tag开发者_运维技巧 = \'img\'; $text = preg_replace(\'#</?\'.$tag.\'[^>]*>#is\', \'\', $text);
$tag开发者_运维技巧 = 'img';
$text = preg_replace('#</?'.$tag.'[^>]*>#is', '', $text);

how can I make preg_replace ignore the first match ("img"), and do the replacing just on the others?


you can use a for() loop and set $i as 1 to avoid the first value in the array, so something like :

 for($i = 1; $tags < count($tags) ; $i++){
 $text = preg_replace('#</?'.$tag.'[^>]*>#is', '', $text[$i]);
 }

but this loop will replace $text value everytime its executed, if you want to save it as a string replace the = sign with .= or if you want to make an array replace $text with $text[$i--]

note that this code is not tested!

0

精彩评论

暂无评论...
验证码 换一张
取 消