[英]How to extract character and number from alphanumeric value using php
I want to extract character and number from a alphanumeric value 我想从字母数字值中提取字符和数字
For example 300G
I want extract 300
and G
as different values 500M : want to 500 and M Please help 例如300G
我想要提取300
和G
作为不同的值500M:想要500和M请帮助
Try with preg_match
: 尝试使用preg_match
:
$input = '300G';
preg_match('/(\d+)(\w)/', $input, $matches);
var_dump($matches);
Output: 输出:
array (size=3)
0 => string '300G' (length=4)
1 => string '300' (length=3)
2 => string 'G' (length=1)
And extra: 额外的:
list(, $digits, $letter) = $matches;
This code should do the trick. 这段代码应该可以解决问题。
$str = '300G';
preg_match("/(\d+)(.)/", $str, $matches);
$number = $matches[1];
$character = $matches[2];
echo $number; // 300
echo $character; // G
$input = '300G';
$number = substr($input, 0, -1);
$letter = substr($input, -1);
use a regular expression 使用正则表达式
$regexp = "/([0-9]+)([A-Z]+)/";
$string = "300G";
preg_match($regexp, $string, $matches);
print_r($matches);
$matches[1] = 300
$matches[2] = G
$matches[1] = 300
$matches[2] = G
$pattern = '#([a-z]+)([\d]+)#i';
if (preg_match($pattern, $str, $matches)){
$letters = $matches[1];
$numbers = $matches[2];
}
Try this, 尝试这个,
<?php
$input = '300G';
preg_match('/(\d+)(\w)/', $input, $matches);
var_dump($matches);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.