Jump to content


stripping all non alpha-numeric characters from string

  • Please log in to reply
2 replies to this topic

#1 bcamp1973

  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 23 August 2006 - 05:07 PM

ok, i want to strip all instances of "\r\n", "\r", "\n","\t" and non-alpha numeric characters from a string. in my searching through the PHP manual there seems to be a lot of options...such as str_replace, preg_replace(), ereg_replace() etc. i need something that's fast an efficient and i'm guessing i'll have to use regex of some form but it's not something i'm very knowledgable of.  any suggestions?

#2 Caesar

  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 23 August 2006 - 06:57 PM

Take the reverse approach, only match those characters you want to retrieve. ;-)
PHP Ninja

#3 obsidian

  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 23 August 2006 - 07:56 PM

i like this approach:
$String = preg_replace('|[^a-z0-9]|i', '', $String);
echo $String;

You can't win, you can't lose, you can't break even... you can't even get out of the game.

while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users