本文介绍了preg_replace除了数字,字母,句号和斜杠以外的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正则表达式,可以除去字母以外的所有内容.数字和句点.我还如何在其中添加斜杠?

I have a regular expression that strips everything but letters. numbers, and periods. How do I also add foreslashes to it?

$targetFile = preg_replace('/[^A-Za-z0-9-.]/', '', $targetFileDirty);

推荐答案

您可以在反斜杠前面加上反斜杠-$targetFile = preg_replace('/[^A-Za-z0-9-.\/]/', '', $targetFileDirty);

You can escape the foreslash by putting a backslash before it - $targetFile = preg_replace('/[^A-Za-z0-9-.\/]/', '', $targetFileDirty);

或者,也许更好,您可以改用其他定界符,例如$targetFile = preg_replace('#[^A-Za-z0-9-./]#', '', $targetFileDirty);

Alternatively, and perhaps better, you can use different delimiters instead, e.g. $targetFile = preg_replace('#[^A-Za-z0-9-./]#', '', $targetFileDirty);

这篇关于preg_replace除了数字,字母,句号和斜杠以外的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 23:55