Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex/PHP删除第一个字母之前的所有内容_Php_Regex - Fatal编程技术网

Regex/PHP删除第一个字母之前的所有内容

Regex/PHP删除第一个字母之前的所有内容,php,regex,Php,Regex,我需要删除第一个字母前的所有字符。。。 如何搜索[a-z]&[a-z]第一次出现的位置 $test = "1234 123423-34 This is a test"; $string = preg_replace('/REGEX/', "", $test); echo $string; 应输出: 这是一个测试使用带前导锚点的否定字符类 ^[^A-Za-z]+ 正则表达式演示: PHP演示: 您可以使用函数返回不匹配任何给定字符的初始字符串段的长度,例如: $position = strc

我需要删除第一个字母前的所有字符。。。 如何搜索[a-z]&[a-z]第一次出现的位置

$test = "1234 123423-34 This is a test";

$string = preg_replace('/REGEX/', "", $test);
echo $string;
应输出:
这是一个测试

使用带前导锚点的否定字符类

^[^A-Za-z]+
正则表达式演示:

PHP演示:

您可以使用函数返回不匹配任何给定字符的初始字符串段的长度,例如:

$position = strcspn($test, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
这不包括Unicode字母

还请注意,您可以使用正则表达式完成任务(替换),而无需确定此位置(请参见其他答案)

您可以尝试以下方法:

<?php
$test = "1234 123423-34 This is a test";
for($i=0;$i<strlen($test);$i++){
    if(!ctype_alpha($test[$i])){
        $test[$i] = '';
    }else{
        break;
    }
}
echo $test;

除了chris85 answer,如果您想处理unicode字符:

$str = '123 μεγάλο τριχωτό της γάτας';
$result = preg_replace('~\PL+~Au', '', $str); 
搜索并替换为空字符串。
$str = '123 μεγάλο τριχωτό της γάτας';
$result = preg_replace('~\PL+~Au', '', $str);