简体   繁体   中英

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: ~ # @ ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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