PHP: How to fix my preg_replace code
If I have a string like
$str = "Hello [Page 1] World [Page 23] Hello [Page 35] World [Page 624]"开发者_高级运维;
And I want to remove all instances of "[Page #]"
How would I go about doing that?
Here's what I've got so far...
preg_replace('[Page [0-9]]', '', $str);
Remember that [ and ] are class delimiters. When you want to use them as literals you need to escape them (prefix with \) first. So:
/\[Page [0-9]+\]/i
is probably your best bet. Surround the pattern with /, add a + to your number range to match "1+ numbers", and the final i means case-insensitive match (P or p in "Page") (remove if that's not your intent).
preg_replace('/\\[Page [0-9]+\\]/', '', $str)
/\[Page \d+\]/
works, too. \d is shorthand for number-chars.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论