我想从我的字符串中删除所有Windows保留字符。我必须删除这些字符:
\,/,:,*,?,",<,>,|, and spaces
如何删除这些字符?
示例:
$string = "21/002-4/ASG* -47";
结果:
"21002-4ASG-47"
答案 0 :(得分:2)
$string = "21/002-4/ASG* -47";
$new = preg_replace('/[\\/\:\*\?\"<>\|\s]+/', '', $string);
echo $new;
可以解决问题,这是一个在线示例:
答案 1 :(得分:1)
您还可以使用preg_replace()
'/[^A-Za-z0-9\-]/'
模式获得结果
$string='21/002-4/ASG* -47';
echo preg_replace('/[^A-Za-z0-9\-]/', '', $string);
结果:
21002-4ASG-47
根据您的评论,如果您只想删除特定字符(已定义),则str_replace
是另一种解决方案。 str_replace
还有另外一件事,很快就会到preg_replace()
。
str_replace()
的示例:
$string='21/002-4/ASG* -47'; // your string
$char = array('/','*',' '); // defined all characters which need to be removed
echo str_replace($char, "", $string); // result should be 21002-4ASG-47