简体   繁体   English

关于使用preg_replace()

[英]Concerning Using preg_replace()

For the purpose of testing, this is the code I'm using on my regular expression: 为了进行测试,这是我在正则表达式上使用的代码:

<?php
function handlePhone($p) {
    return preg_replace("[^0-9xX\+]", "", $p);
}

echo handlePhone("+44(324)-s123-32yousuck42x123");
?>

Right now, all that's printing is that exact string as if no changes were made. 现在,所打印的只是该字符串,就好像没有进行任何更改一样。 Any ideas why? 有什么想法吗?

You must use delimiters at the end and begining of your pattern, example with / : 您必须在模式的末尾使用定界符,例如/

function handlePhone($p) {
    return preg_replace("/[^0-9xX\+]/", "", $p);
}

Note that you can use other delimiters: ~ # @ ... 请注意,您可以使用其他定界符: ~ # @ ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM