要打印不需要的字符,请使用preg_match_all()。假设以下是我们的带有一些特殊字符的字符串-
$sentence= "M-y Name/i_s John+Doe";
我们希望输出仅显示上述字符串中的特殊字符,即
-/_+
PHP代码如下
<!DOCTYPE html>
<html>
<body>
<?php
$sentence= "M-y Name/i_s John+Doe";
echo "The original value is=",$sentence,"<br>";
if(preg_match_all('/[^a-zA-Z ]/', $sentence, $output) ){
echo "The unwanted characters are as follows= " . implode('', $output[0]);
}
?>
</body>
</html>输出结果
这将产生以下输出
The original value is=M-y Name/i_s John+Doe The unwanted characters are as follows= -/_+