在php中,如果在下划线之前找到一系列数字,如何将其去掉

在php中,如果在下划线之前找到一系列数字,如何将其去掉,php,regex,Php,Regex,假设我有以下字符串: $string = "hello1711089_world86"; 及 输出应为-> $x = "hello1711089"; 及 想要在两种情况下都可以使用的单一代码 尝试使用以下正则表达式: \w.*\d{7} 代码如下: $re = '/\w.*\d{7}/m'; $str = 'hello1711089_world86 hello_something_1711089_world86_quotes'; preg_match_all($re, $str, $m

假设我有以下字符串:

$string = "hello1711089_world86";

输出应为->

$x = "hello1711089";

想要在两种情况下都可以使用的单一代码

尝试使用以下正则表达式:

\w.*\d{7}
代码如下:

$re = '/\w.*\d{7}/m';
$str = 'hello1711089_world86

hello_something_1711089_world86_quotes';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);
使用函数


纯代码编写请求与堆栈溢出无关-我们希望这里的问题与特定的编程问题相关-但我们很乐意帮助您自己编写!告诉我们你尝试了什么,你被困在哪里了。有关更多信息,请参阅。这也将有助于我们更好地回答您的问题。您也可以使用“是”或“我做了”。你有没有用这个字符串hello17119_world86?OP没有指定它将有7个确切的数字,它没有只在帖子中说这个词。上面写着,我有这根绳子。这意味着字符串可能会更改。
\w.*\d{7}
$re = '/\w.*\d{7}/m';
$str = 'hello1711089_world86

hello_something_1711089_world86_quotes';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);
$string ="hello_something_1711089_world86_quotes";
$string = explode("_",$string);
echo $string[0]; //hello