更灵活的方式来做多个 str_replace

slicker way to do a mulitple str_replace

使用 str_replace 更改多个项目的最佳方法是什么,而不是以下方法:

$dataMeta = str_replace(['fooboy_','foogirl_','foonut_'],['','',''],$source);

例如....

变化: fooboy_1234 | foogirl_5678 | foonut_0909

收件人: 1234 | 5678 | 0909

改用preg_replace

$string = 'foo_3456';
echo preg_replace('/[a-z]+_(\d+)/i', '', $string);

实例here

因此,使用这种简单的方法,您可能会将其应用于字符串数组,例如使用 array_map 函数

$strings = ['foo_1234', 'bar_3456', 'foo_5678', 'bar_7890'];
$strings = array_map(
    function($string){
        return preg_replace('/[a-z]+_(\d+)/i', '', $string);
    },
    $strings
);
var_dump($strings);

实例here