Replace Whitespace with Hyphen

Hi all, how can I replace white-space with a hyphen AND also strip all alphanumeric characters? I’ve managed to replace all white-space. Don’t really want to use two of these functions as I have heard regex can be pretty resourceful.

$clean_url = ereg_replace("[^A-Za-z0-9-]", "-", $prodList['products_name']);

ereg is deprecated, so if you’re gonna use a regex function it’s time to look at preg :wink:

[FPHP]preg_replace[/FPHP] can take arrays as it’s arguments. Stick your two patterns in an array (an inline array works fine), your two replacements in another, stick them into preg_replace, and away it goes.

(Just make sure that your replacement array lines up with your pattern array! $pattern[0] will be replaced by $replace[0], etc.)

I probably would have used str_replace, but this sounds like a better solution, not bad at all.

Here’s a preg_replace() equivalent (same expression but is surrounded by ‘/’ characters):

$clean_url = preg_replace('/[^A-Za-z0-9-]/', "-", $prodList['products_name']);

But that can leave you with lots of miscellaneous hyphens. You might like this better:

$clean_url = preg_replace('/\\s+/', "-", trim(preg_replace('/[^A-Za-z0-9]/', " ", $prodList['products_name'])));

Replaces every non-alphanumeric character with a space, trims any surrounding whitespace, and then makes spaces into hyphens while simultaneously reducing multiple spaces to a single hyphen.


You’re removing all alphanumeric characters with that first part. I’m pretty sure the OP doesn’t mean to do that despite initially saying they did want to. The initial example clearly indicates keeping alphanumeric characters around. The variable name also indicates the intent to get something safe for use in a URL without having to do any funky encoding later on.

if by ‘funky encoding’ you mean throwing [FPHP]urlencode[/FPHP] around the string, then… yeah, i guess. Personally dont really see that being a huge inconvenience…

It also strikes me that the thing he’s trying to url-encode-without-url-encoding is a database string, and probably could be replaced by something shorter and not-needing-encoding-worries.

as far as “He wanted to keep the alphanums”… so stick a ^ in front of the class and flip the array elements.

What is wrong with just doing this:

preg_replace( '/[^\\w]+/', '-', $input );
# or...
preg_replace( '/[^a-zA-Z0-9]+/', '-', $input );

Should also keep “----” from appearing.

\w matches _ (undesired by OP), and may match additional characters depending on locale.