Saturday, July 4th, 2009

Match non-alphanumeric characters from a string

by Gabriel on 05/10/08 at 6:57 am

Save to StumbleUpon Stumble Upon it!     Save to Del.icio.us Save to Del.icio.us    Share on Twitter! Share on Twitter!

Greetings! Subscribe to my RSS feed or get my latest post directly in your mailbox. Thanks for visiting!

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] => $
)
Be notified when we have new posts by subscribing to BitRepository RSS Feed.
Support us!Did you like this post?
Please spread the word!
Save to StumbleUpon  Save to Del.icio.us  Share on Twitter!    

Leave a Comment