Developers Heaven Forum

Web Programming => PHP & Perl => Topic started by: admin on August 22, 2009, 04:41:18 PM

Title: preg_replace allow space
Post by: admin on August 22, 2009, 04:41:18 PM
$pattern = "/[^\d\w\s]/";
$replacement = "*";
$subject = "1 Main str, #5";

echo preg_replace($pattern, $replacement, $subject);

The ^ symbol negate a class. Right after this symbol we put the classes:

\d - digits
\w - words
\s - space

So, it reads as: "replace with * all the symbols from $subject, which are not digits, words and spaces".