2013-10-27 17 views
0

考虑例如:[解决]修改变量字符串值用PHP

$mystring = "us100ch121jp23uk12"; 

I)I希望通过添加1改变jp值,以便使串入

us100ch121jp24uk12 

假设if

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>"; 
} 
+2

是否有可能修改字符串输出?可以有更好的方式来表示数据 - 即在一个数组中,使用键值对。至少,在每个国家/地区评分对之后添加分隔符,例如'us100&ch121&jp23&uk12'或类似物(尽管这是_still_ bad设计)。 – Terry

回答

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的更多信息。该网站还包含有关正则表达式的信息,对于任何程序员来说都是一个有用的工具!

+0

您好先生,谢谢,这是帮助我,但是有没有一种方法可以通过他们,以便我可以得到问题的第二部分? – user2849437

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