考虑示例:
$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);
使用埃文先生的建议
$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>";
}
如果使用 preg_match_all("/([A-z]+)('d+)/", $string, $output);
,它将向$output
返回一个包含三个数组的数组。第一个数组将是国家/地区数字字符串(例如 'us100'
)。第二个将包含国家/地区字符串(例如 'us'
)。第三个将包含数字(例如'100'
)。
由于第二个和第三个数组将具有匹配的索引($output[1][0]
将'us'
,$output[2][0]
将被'100'
),您可以循环浏览这些索引并对它们执行任何您想做的事情。
以下是有关在 PHP 中使用正则表达式的更多信息。该网站还包含有关正则表达式的一般信息,这对任何程序员来说都是一个有用的工具!
你可以在 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