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: 265
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:
- steel-black
- steel- black
- steel black
I should also remove any of these as well "\r\n\t".
© Stack Overflow or respective owner