Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP将值1添加到字符串中的一个位置并替换_Php_Function - Fatal编程技术网

PHP将值1添加到字符串中的一个位置并替换

PHP将值1添加到字符串中的一个位置并替换,php,function,Php,Function,我想用另一个字符替换一个字母,并再增加1个值。 我的意思是价值观是动态的 例如,我有H9,我想替换为G10 同样地 H2asG3 H6asG7 是否可以使用str\u replace()进行此操作?当然可以使用 使用 其中,$string是您的原始字符串。试试这个 <?php $str = "H25"; preg_match_all('!\d+!', $str, $matches); $str = str_replace($matches['0']['0'], $matches['0

我想用另一个字符替换一个字母,并再增加1个值。 我的意思是价值观是动态的

例如,我有
H9
,我想替换为
G10

同样地

  • H2
    as
    G3
  • H6
    as
    G7
是否可以使用
str\u replace()
进行此操作?

当然可以使用

使用

其中,
$string
是您的原始字符串。

试试这个

<?php 
$str = "H25";
preg_match_all('!\d+!', $str, $matches);
$str = str_replace($matches['0']['0'], $matches['0']['0']+1, $str, $count);
echo $str;
?>

我买了一个适合我的:

$old_string = "H2";
$new_string = "G" . (substr($old_string, 1) + 1);
echo $new_string;
对我来说很好。这只是一个值,但我想你也可以循环一个数组,只需要像这样修改值

foreach($old_values as $v) {
    $new_values[] = "G" . (substr($v, 1) + 1);
}
因此,您可以将所有值保存到$new_字符串数组中

更简单的方法。。。(广义解)

foreach($old_values as $v) {
    $new_values[] = "G" . (substr($v, 1) + 1);
}
<?php
$str='G10';
preg_match('!\d+!', $str, $digit); //<---- Match the number
$str = substr($str,0,1); //<--- Grab the first char & increment it in the second line
echo ++$str.($digit[0]+1); //"prints" H11