Forum Moderators: coopster
(spaces resresented by dashes)
This-is-good. => This-is-good.
This-is--not. => This-is-not.
This-is---not. => This-is-not.
This\n
is\n
good.
This\n
\n
is\n
\n
\n
\n
not.
This\n
-\n
-\n
is\n
---\n
not.
I know its something further than:
str_replac(array("--","\n\n"),array("-","\n"),$text);
Any help would greatly be appreciated.
Thanks.
<?$pattern = array("/\s{2,}/", "/\-{2,}/", "/\n{2,}/"); //searches for dashes, spaces, newlines in succession
$replace= array(" ","-","\n");
$text = preg_replace($pattern, $replace, $text);
?>
$pattern = '%([\s-])+%';
$text = preg_replace($pattern, '$1', $text);
You can add anything you want to the character class in the $pattern, as if it is repeated the repeat character will be removed.
[edited by: PHP_Chimp at 7:06 pm (utc) on Dec. 14, 2007]
I wasn't trying to match the dashes, I just wanted to show that there might be more than one in a row. I thought that if I had just typed it in, the HTML would just register it as one. So it looks like I can downsize the matching string to just "%(\s)+%".
May I ask what the quotes do? I understand everything else but that part.
Also, I didn't realize you can insert the matching parts back in for the replacement. I've been using the matches in the htaccess files for rewriting urls, but using it in php is a revelation to me.