<?php
$t="123xxx=987&sh=2";
$rand=strval(rand(1,99999999));
$x=preg_replace("{(.+)(xxx=)(.+?)(&sh)}", "$1$2$rand$4", $t);
print_r($x);preg_match_all("{(.+)(xxx=)(.+?)(&sh=)}",$t,$re);
print_r($re);
?>
我想把123xxx=987&sh=2中的987换成一个随机数,但是怎么不对?
我测试输出$x的时候成了“随机数&sh=2”。"123xxx="这几个字不见了?但是我查看$re的时候,子文本却是正常的,怎么回事这是?
[1] => Array
(
[0] => 123
) [2] => Array
(
[0] => xxx=
) [3] => Array
(
[0] => 987
) [4] => Array
(
[0] => &sh=
)
$t="123xxx=987&sh=2";
$rand=strval(rand(1,99999999));
$x=preg_replace("{(.+)(xxx=)(.+?)(&sh)}", "$1$2$rand$4", $t);
print_r($x);preg_match_all("{(.+)(xxx=)(.+?)(&sh=)}",$t,$re);
print_r($re);
?>
我想把123xxx=987&sh=2中的987换成一个随机数,但是怎么不对?
我测试输出$x的时候成了“随机数&sh=2”。"123xxx="这几个字不见了?但是我查看$re的时候,子文本却是正常的,怎么回事这是?
[1] => Array
(
[0] => 123
) [2] => Array
(
[0] => xxx=
) [3] => Array
(
[0] => 987
) [4] => Array
(
[0] => &sh=
)
$x=preg_replace("{(\d+)(xxx=)(.+?)(&sh)}", "$1$2$3$rand$4", $t);代码改成这样的时候就能达到你的预期目的了..至于为什么是这样,楼下回答 = =~
确实可以了,但是如果前面不是123,而是
$t="1s2g3xxx=987&sh=2";怎么办?
改成 $x=preg_replace("{(.+)(xxx=)(.+?)(&sh)}", "$1\${2}{$rand}$4", $t);