0

Consider example:

$mystring = "us100ch121jp23uk12";

I) I want to change value of jp by adding +1 so that makes the string into

us100ch121jp24uk12

suppose if

II) Is there a way to seperate the numeric part and alphabetic part in the above string into:

[us , 100]
[ch,121]
[jp,24]
[us,12]

my code:

$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);

using Evan sir's suggestions

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

2 回答 2

2

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

由于第二个和第三个数组将具有匹配的索引($output[1][0]will be'us'$output[2][0]will be '100'),因此您可以循环浏览这些索引并对它们做任何您想做的事情。

这是有关在 PHP 中使用正则表达式的更多信息。该站点还包含有关一般正则表达式的信息,这对任何程序员来说都是一个有用的工具!

于 2013-10-27T16:21:38.350 回答
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

于 2013-10-27T16:22:58.013 回答