PHP preg_replace, need a REGEX to replace "endofentry" + number + " />"
I need a quick REGEX to use with preg_replace() in PHP that will remove instances of the string...
"endofentry" + followed by any three digit number + " />" tacked on the end.
any help would be fantastic, thanks 开发者_开发技巧a lot!
Try this:
preg_replace('/endofentry\\d{3}\\/>/', '', 'endofentry321/> asdfa s');
Tested here
If you need to extract the three digit number, then this will work:
/(?<=endofentry)[0-9]{3}(?=\/>)/
As in:
<?php
CONST REGEX = "/(?<=endofentry)[0-9]{3}(?=\/>)/";
$stringSubject = "endofentry456/>";
preg_match(REGEX, $stringSubject, $match);
echo $match[0]; //echo's 456
?>
If you need to replace the entire string:
Update
<?php
CONST REGEX = "/\"endofentry\"[0-9]{3}\/>/";
$stringSubject = "\"endofentry\"567/>";
$stringReplace = "replace_me!";
echo preg_replace(REGEX, $stringReplace, $stringSubject); //echo's replace_me!
?>
精彩评论