简体   繁体   中英

PHP Regex Replace Number in String

I'm trying to replace a number in a string with another number by using regex. This should be super simple, but for some reason it's giving me grief today. "feed" in the string is dynamic and can contain letters or numbers only.

Here is what I've tried:

$new_number = 999;
$string = '/feed/page/1/?param=true';
preg_replace('/\/([0-9a-z])\/page\/([0-9])\/(.*)/i', '/$1/page/'.$new_number.'/$3', $string);

Should output: '/feed/page/999/?param=true'

forgetting the +'s, are you? :P Try this as your regEx:

'/\/([0-9a-z]+)\/page\/([0-9]+)\/(.*)/i' 

尝试这个:

\/([0-9a-z]{1,})\/page\/([0-9]{1,})\/(.*)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM