使用php修改变量字符串中的值

使用php修改变量字符串中的值,php,string,variables,match,Php,String,Variables,Match,举个例子: $mystring = "us100ch121jp23uk12"; 一) 我想通过添加+1来更改jp的值,使字符串变成 us100ch121jp24uk12 假设 二) 有没有办法将上述字符串中的数字部分和字母部分分为: [us , 100] [ch,121] [jp,24] [us,12] 我的代码: $string = "us100ch121jp23uk12"; $search_for = "us"; $pairs = explode("[]", $string); //

举个例子:

$mystring = "us100ch121jp23uk12";
一) 我想通过添加+1来更改
jp
的值,使字符串变成

us100ch121jp24uk12
假设

二) 有没有办法将上述字符串中的数字部分和字母部分分为:

[us , 100]
[ch,121]
[jp,24]
[us,12]
我的代码:

$string = "us100ch121jp23uk12";

$search_for = "us";
$pairs = explode("[]", $string); // I dont know the parameters.
foreach ($pairs as $index=>$pair)
{
    $numbers = explode(',',$pair);
    if ($numbers[0] == $search_for){

        $numbers[1] += 1; // 23 + 1 = 24
        $pairs[index] = implode(',',$numbers); //push them back
        break;
    }
}
$new_string = implode('|',$pairs);
使用Evan sir的建议

$mystring = "us100ch121jp22uk12";

preg_match_all("/([A-z]+)(\d+)/", $mystring, $output);

//echo $output[0][4];


foreach($output[0] as $key=>$value) {
   // echo "[".$value."]";
   echo "[".substr($value, 0, 2).",".substr($value, 2, strlen($value) - 2)."]"."<br>";
}
$mystring=“us100ch121jp22uk12”;
preg_match_all(“/([A-z]+)(\d+/”,$mystring,$output);
//echo$输出[0][4];
foreach($key=>$value形式的输出[0]{
//回声“[”$value.“]”;
回声“[”.substr($value,0,2)。”,“.substr($value,2,strlen($value)-2)。”]”;
}

如果使用
preg\u match\u all(“/([A-z]+)(\d+/”,$string,$output)
,它将返回一个包含三个数组的数组到
$output
。第一个数组将是国家编号字符串(例如
'us100'
)。第二个将包含国家字符串(例如
'us'
)。第三个将包含数字(例如
'100'

由于第二个和第三个数组将有匹配的索引(
$output[1][0]
将是
'us'
,而
$output[2][0]
将是
'100'
),您可以循环使用这些索引并对其执行任何操作


这里有更多的信息。该网站还包含关于正则表达式的一般信息,对于任何程序员来说,正则表达式都是有用的工具

您可以在PHP中使用正则表达式来实现。请参见教程:

功能描述
ereg_replace()函数用于查找模式指定的字符串,如果找到,则将模式替换为替换。
eregi_replace()函数的工作原理与ereg_replace()类似,只是在字符串中搜索模式不区分大小写。
preg_replace()preg_replace()函数的工作原理与ereg_replace()类似,只是正则表达式可以在模式和替换输入参数中使用。
preg_match()函数用于查找模式的字符串,如果模式匹配false,则返回true,否则返回true。
表达式描述
[0-9]它匹配0到9之间的任何十进制数字。
[a-z]它匹配从小写a到小写z的任何字符。
[A-Z]它匹配从大写字母A到大写字母Z的任何字符。
[a-Z]它匹配从小写字母a到大写字母Z的任何字符。
p+它匹配任何至少包含一个p的字符串。
p*它匹配任何包含零个或多个p的字符串。
P它匹配任何包含零个或多个p的字符串。这只是使用p*的另一种方法。
p{N}它匹配任何包含np序列的字符串
p{2,3}它匹配任何包含两个或三个p的序列的字符串。
p{2,}它匹配任何包含至少两个p的序列的字符串。
p$它匹配任何结尾带有p的字符串。
^p它将任何字符串的开头与p匹配。
[^a-zA-Z]它匹配任何不包含从a到Z以及从a到Z的任何字符的字符串。
p、 它匹配任何包含p的字符串,后跟任何字符,然后再跟另一个p。
^.{2}$它匹配任何正好包含两个字符的字符串。
(*)它匹配和中包含的任何字符串。
p(hp)*它匹配任何包含p后跟零个或多个序列hp实例的字符串。
您还可以使用JavaScript:

是否可以修改字符串输出?可能有更好的方法来表示数据-例如,在数组中,使用键值对。至少,在每个国家/地区的分数对后添加分隔符,例如
us100&ch121&jp23&uk12
或类似的东西(尽管这仍然是一个糟糕的设计)。您好,先生,谢谢,这对我有帮助,但是有没有办法让我循环通过它们,以便我可以像问题的第二部分一样?
Function    Description
ereg_replace()  The ereg_replace() function finds for string specified by pattern and replaces pattern with replacement if found.
eregi_replace() The eregi_replace() function works similar to ereg_replace(), except that the search for pattern in string is not case sensitive.
preg_replace()  The preg_replace() function works similar to ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters.
preg_match()    The preg_match() function finds string of a pattern and returns true if pattern matches false otherwise.
Expression  Description
[0-9]   It matches any decimal digit from 0 through 9.
[a-z]   It matches any character from lowercase a through lowercase z.
[A-Z]   It matches any character from uppercase A through uppercase Z.
[a-Z]   It matches any character from lowercase a through uppercase Z.
p+  It matches any string containing at least one p.
p*  It matches any string containing zero or more p’s.
p?  It matches any string containing zero or more p’s. This is just an alternative way to use p*.
p{N}    It matches any string containing a sequence of N p’s
p{2,3}  It matches any string containing a sequence of two or three p’s.
p{2, }  It matches any string containing a sequence of at least two p’s.
p$  It matches any string with p at the end of it.
^p  It matches any string with p at the beginning of it.
[^a-zA-Z]   It matches any string not containing any of the characters ranging from a through z and A through Z.
p.p It matches any string containing p, followed by any character, in turn followed by another p.
^.{2}$  It matches any string containing exactly two characters.
<b>(.*)</b> It matches any string enclosed within <b> and </b>.
p(hp)*  It matches any string containing a p followed by zero or more instances of the sequence hp.