删除最后一个下划线之前的所有字符串

rus*_*sly 1 php preg-replace

我尝试在下划线破折号之后获取姓氏

$x = "John_Chio_Guy";
Run Code Online (Sandbox Code Playgroud)

输出应该是:Guy

这是我当前的代码:

$x = "John_Chio_Guy";
$x = preg_replace("/^[^_]*_\s*/", "", $x);
echo $x;
//output : Chio_Guy
Run Code Online (Sandbox Code Playgroud)

Sun*_*dar 5

你可以使用爆炸功能,这太简单了

<?php

$x = "John_Chio_Guy";

//explode the string
$explode = explode('_', $x);

//get the end value
echo end($explode);

?>
Run Code Online (Sandbox Code Playgroud)