开发者

php regex - Delete phrase

I have a long string that has imbedded in it: "ABC_1_" and "ABC_2_" and "ABC_3_" etc.

For example:

"lorum ipsum ABC_1_ doit more ABC_3_ and so on".

I need to write a PHP preg_replace that will remove the 6 characters (ABC_xx) if the first 4 are "ABC_" and return the full remaining stri开发者_开发百科ng, in my example:

"lorum ipsum  doit more  and so on". 

Thanks!


Use preg_replace with regular expression: (ABC_.{2} )

$string = "lorum ipsum ABC_1_ doit more ABC_3_ and so on";
$pattern = "/(ABC_.{2})/";
$replacement = "";
echo preg_replace($pattern, $replacement, $string);


Try this:

$s = preg_replace('/\bABC_../', '', $s);

The \b matches a word boundary, and the dots match any character (apart from new line).

Full example:

<?php
$s = 'ABC_1_foo lorum ipsum ABC_1_ doit more ABC_3_ and so on'; 
$s = preg_replace('/\bABC_../', '', $s);
echo $s;
?>

Result:

foo lorum ipsum  doit more  and so on

(ideone)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜