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

时间:2013-10-27 16:14:52

标签: php string variables match

考虑例子:

$mystring = "us100ch121jp23uk12";

I)我想通过添加+1来更改jp的值,以便将字符串转换为

us100ch121jp24uk12

假设是否

II)是否有办法将上述字符串中的数字部分和字母部分分隔为:

[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先生的建议

$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>";
}

2 个答案:

答案 0 :(得分:2)

如果您使用preg_match_all("/([A-z]+)(\d+)/", $string, $output);,它将返回一个包含三个数组的$output数组。第一个数组将是国家/地区编号字符串(例如'us100')。第二个将包含国家/地区字符串(例如'us')。第三个将包含数字(例如'100')。

由于第二个和第三个数组将具有匹配的索引($output[1][0]将为'us'$output[2][0]将为'100'),因此您可以循环使用这些索引并执行任何操作你想要他们。

以下是有关using regular expressions in PHP的更多信息。该网站还包含有关正则表达式的一般信息,对于任何程序员来说都是有用的工具!

答案 1 :(得分:0)

您可以使用PHP中的正则表达式来完成此操作。见教程: http://w3school.in/w3schools-php-tutorial/php-regular-expression/

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.

你也可以使用JavaScript: http://www.w3schools.com/jsref/jsref_obj_regexp.asp