去除所有字符串的奇怪字符

I'm trying to create a slug so I would like to strip out every strange character. The only thing the slug should contain is lowercase letters and underscores. Is there a way to check for strange characters and filter it out the string? everything that is not a character or underscore should be deleted

this is what I have:

if(!preg_match_all('/[a-z]/')):
    $output = preg_replace("/ ... euhm ... /", "", $slug2);
else:
    $output = $slug2;
endif;

I should go from this: Create a 3D Ribbon Wrap-Around Effect (Plus a Free PSD!)

to this: create_a_3d_ribbon_wrap_around_effect_plus_a_free_psd

$slug = strtolower($slug);
$slug = str_replace(" ", "_", $slug);
$slug = preg_replace("/[^a-z0-9_]/", "", $slug);
$slug = preg_replace("[^a-z_]", "", $slug);

No need for the initial match. You can do an unconditional search-and-replace. If there's nothing to replace, no big deal. Here it is as one big chain of function calls:

$slug = trim(preg_replace('/[\W_]+/', '_', strtolower($slug)), '_');

Or split out into separate lines:

$slug = strlower($slug);
$slug = preg_replace('/[\W_]+/', '_', $slug);
$slug = trim($slug, '_');

Explanation:

  1. Convert uppercase to lowercase with strtolower.
  2. Search for \W and _. A "word" character is a letter, digit, or underscore. A "non-word" character is the opposite of that, i.e. whitespace, punctuation, and control characters. \W matches "non-word" characters.
  3. Replace those bad characters with underscores. If there's more than one in a row they'll all get replaced by a single underscore.
  4. Trim underscores from the beginning and end of the string.

The code's on the complicated side because there are several tricky cases it needs to handle:

  • Bad characters on the ends need to be deleted, not converted to underscores. For example, the !) in your example.
  • We want foo_-_bar to turn into foo_bar, not foo___bar. Underscores should be collapsed, basically.