Reader
  • Home
  • About
  • Deals
  • Services
  • Contact

PHP: Match Non-Alphanumeric Characters from a String

Last updated on December 30th, 2009 by Gabriel Livan

This is a snippet that matches non-alphanumeric characters from a string (text).

<?php
function match_non_alphanumeric_characters($string, $strip_space = true) // Match spaces?
{
$str = ($strip_space) ? ' ' : '';
preg_match_all('/([^a-zA-Z0-9'.$str.']+)/', $string, $match);

return $match[0];
}

$string = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent porttitor euismod enim # Sed posuere & Duis non elit * Sed tempus dolor$';

$non_alphanumeric_array = match_non_alphanumeric_characters($string);

echo '<pre>'; print_r($non_alphanumeric_array); echo "</pre>";
?>

Output:

Array
(
    [0] => ,
    [1] => .
    [2] => #
    [3] => &
    [4] => *
    [5] => $
)

Filed Under: PHP Tagged With: extract

About Me

Gabriel LivanHey, there! I'm Gabriel Livan, Web Developer, Blogger and Internet Marketer. I love making fast, secure and scalable web applications, running and travelling around the world. I blog about WordPress, site speed & security and quality tools for web professionals. Read more

What are you looking for?

Categories

  • AJAX (22)
  • Deals (10)
  • Design (30)
  • eBooks (2)
  • Flash (19)
  • Freebies (11)
  • General (62)
  • Giveaways (1)
  • Graphics (27)
  • HTML & CSS (35)
    • Frameworks (9)
  • JavaScript (160)
    • Frameworks (8)
    • jQuery (89)
    • MooTools (14)
  • Page Loading Speed (2)
  • Photoshop (3)
  • PHP (113)
  • Reviews (8)
  • SEO (6)
  • Statistics (2)
  • Web Tools (13)
  • WordPress (27)
    • Plugins (15)
    • Snippets (4)
    • Tools (2)
    • Tutorials (6)

Copyright © 2026 · BitRepository.com - All Rights Reserved

  • Home
  • About
  • Contact