php regex replace slashes and spaces and hyphens

Posted by Muhammad Shahzad on Stack Overflow See other posts from Stack Overflow or by Muhammad Shahzad
Published on 2014-08-24T16:08:30Z Indexed on 2014/08/24 16:20 UTC
Read the original article Hit count: 269

Filed under:
|

I have fews combinations of strings that I need to bring in to one pattern, that is multiple spaces should be removed, double hyphens should be replaced with single hypen, and single space should be replaced with single hyphen.

I have already tried this expression.

$output = preg_replace( "/[^[:space:]a-z0-9]/e", "", $output );
$output = trim( $output );
$output = preg_replace( '/\s+/', '-', $output );

But this fails in few combinations.

I need help in making all of these combinations to work perfectly:

  1. steel-black
  2. steel- black
  3. steel black

I should also remove any of these as well "\r\n\t".

© Stack Overflow or respective owner

Related posts about php

Related posts about regex