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:
strtolower
.\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.The code's on the complicated side because there are several tricky cases it needs to handle:
!)
in your example.foo_-_bar
to turn into foo_bar
, not foo___bar
. Underscores should be collapsed, basically.