简体   繁体   English

PHP正则表达式替换字符串中的数字

[英]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. 字符串中的“ feed”是动态的,只能包含字母或数字。

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' 应该输出:'/ feed / page / 999 /?param = true'

forgetting the +'s, are you? 忘了+,是吗? :P Try this as your regEx: :P试试这个作为您的regEx:

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

尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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