Replace all characters except Numbers Letters and space

Using ereg_replace I want to replace all HTML characters the only characters i want is ALPHA Numeric and space
Who is Participating?
TheAnarchistConnect With a Mentor Commented:
Correction...remove the double quote from that...otherwise it will preserve those as well...
$my_string = eregi_replace('[^a-z0-9 ]', '', $my_string);

Open in new window

This will remove anything that is not a letter, a number, or a space.
$my_string = eregi_replace('[^a-z0-9 "]', '', $my_string);

Open in new window


You can also try the preg_replace which is often faster than ereg_replace.

I am not sure if you need to use multiline mode. If yes, just change expression to

$pattern = '/[^a-z0-9\s]/im';
$string = 'yourHTML';
$pattern = '/[^a-z0-9\s]/i';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.