• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2038
  • Last Modified:

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
0
stargateatlantis
Asked:
stargateatlantis
  • 2
1 Solution
 
TheAnarchistCommented:
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

0
 
TheAnarchistCommented:
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

0
 
NicksonKohCommented:
Hi,

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';
<?php
$string = 'yourHTML';
$pattern = '/[^a-z0-9\s]/i';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
?>

Open in new window

0
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.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now