这就是我所拥有的--华为MatePad 11 (2021)
这就是我想要的-华为-MatePad-11-2021
这就是我到目前为止所做的。(只是它将“空格”替换为"-“。
echo preg_replace('/[[:space:]]+/', '-', $test);我想删除圆括号,并想立即用"-“替换”空格“。该怎么做呢?
发布于 2021-07-06 14:22:34
$text = 'Huawei MatePad 11 (2021)';
$text = preg_replace('/[(|)]/', '', preg_replace('/\s+/', '-', $text));
echo($text);发布于 2021-07-06 14:27:10
$test = preg_replace('/[[:space:]]+/', '-', $test); # Replace space
$test = preg_replace('/[\(|\)]+/', '', $test); # Replace parenthesis
echo $test;发布于 2021-07-06 15:02:22
您也可以将单个模式与preg_replace_callback一起使用,并从开始到结束括号进行匹配。
\(([^()]+)\)|\h+模式匹配:
\( match (([^()]+) )\) )| Or\h+ Match 1 Match 1+ chars ( )\) Match )|Or\h+Match 1 or more horizontal ( chars例如,如果存在,则使用$m[1]返回group 1,否则返回-
$s = "Huawei MatePad 11 (2021)";
$regex = "/\(([^()]+)\)|\h+/";
$result = preg_replace_callback($regex, function($m) {
return array_key_exists(1, $m) ? $m[1] : '-';
}, $s);
echo $result;输出
Huawei-MatePad-11-2021https://stackoverflow.com/questions/68265247
复制相似问题