php 如何在php中从字符串中分离字母和数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4311156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:36:51  来源:igfitidea点击:

How to separate letters and digits from a string in php

phpstringsubstring

提问by sandeep

I have a string which is combination of letters and digits. For my application i have to separate a string with letters and digits: ex:If my string is "12jan" i hav to get "12" "jan" separately..

我有一个字符串,它是字母和数字的组合。对于我的应用程序,我必须用字母和数字分隔一个字符串:例如:如果我的字符串是“12jan”,我必须分别得到“12”“jan”..

回答by codaddict

You can make use of preg_splitto split your string at the point which is preceded by digit and is followed by letters as:

您可以利用preg_split在数字前面和字母后面的点处拆分字符串,如下所示:

$arr = preg_split('/(?<=[0-9])(?=[a-z]+)/i',$str);

Code in Action

行动中的代码

<?php
$str = '12jan';
$arr = preg_split('/(?<=[0-9])(?=[a-z]+)/i',$str); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
print_r($arr);

Result:

结果:

Array
(
    [0] => 12
    [1] => jan
)

回答by Mike C

$numbers = preg_replace('/[^0-9]/', '', $str);
$letters = preg_replace('/[^a-zA-Z]/', '', $str);

回答by Breezer

$string = "12312313sdfsdf24234";
preg_match_all('/([0-9]+|[a-zA-Z]+)/',$string,$matches);
print_r($matches);

this might work alot better

这可能会更好

回答by alex

preg_match_all('/^(\d+)(\w+)$/', $str, $matches);

var_dump($matches);

$day = $matches[1][0];
$month = $matches[2][0];

Of course, this only works when your strings are exactly as described "abc123"(with no whitespace appended or prepended).

当然,这仅在您的字符串完全符合描述的“abc123”(没有附加或前置空格)时才有效。

If you want to get all numbers and characters, you can do it with one regex.

如果你想得到所有的数字和字符,你可以用一个正则表达式来完成。

preg_match_all('/(\d)|(\w)/', $str, $matches);

$numbers = implode($matches[1]);
$letters = implode($matches[2]);

var_dump($numbers, $letters);

See it!

看见!

回答by user3230794

This works for me as per my requirement, you can edit as per yours

这根据我的要求对我有用,您可以根据您的要求进行编辑

function stringSeperator($string,$type_return){

    $numbers =array();
    $alpha = array();
    $array = str_split($string);
    for($x = 0; $x< count($array); $x++){
        if(is_numeric($array[$x]))
            array_push($numbers,$array[$x]);
        else
            array_push($alpha,$array[$x]);
    }// end for         

    $alpha = implode($alpha);
    $numbers = implode($numbers);

    if($type_return == 'number')    
    return $numbers;
    elseif($type_return == 'alpha')
    return $alpha;

}// end function

回答by Anton Kiggundu

Having worked more with PHPExcel, such operations are common. #Tapase,. Here is a preg_split that gets you want you want with space within the string.

使用 PHPExcel 后,此类操作很常见。#Tapase,。这是一个 preg_split ,它让你想要你想要的字符串中的空间。

<?php
$str = "12 January";
$tempContents = preg_split("/[\s]+/", $str);
foreach($tempContents as $temp){
echo '<br/>'.$temp;
}
?>

You can add a comma next to the sfor comma seperated. Hope it helps someone. Anton K.

您可以在s旁边添加一个逗号来分隔逗号。希望它可以帮助某人。安东 K。

回答by Anton Kiggundu

Try This :

尝试这个 :

$string="12jan";
$chars = '';
$nums = '';
for ($index=0;$index<strlen($string);$index++) {
    if(isNumber($string[$index]))
        $nums .= $string[$index];
    else    
        $chars .= $string[$index];
}
echo "Chars: -$chars-<br>Nums: -$nums-";


function isNumber($c) {
    return preg_match('/[0-9]/', $c);
} 

回答by Gēoffrey Eng ?tkinson

<?php
$data = "#c1";
$fin =  ltrim($data,'#c');
echo $fin;
?>