note 124771 deleted from function.preg-replace by crell
| From: | crell@php.net | Date: | Fri, 01 Jul 2022 19:43:37 +0000 |
| Subject: | note 124771 deleted from function.preg-replace by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218847@lists.php.net to get a copy of this message | ||
Note Submitter: Ducktales
----
To replace double whitespace by a single space character in a sentence you need to use the \h
modifier. It stands for horizontal whitespace. The more common \s modifier includes vertical
whitespace such as line breaks, which is not good for saving paragraphs.
Additionally, since most of todays code works with unicode strings you need to add the /u unicode
modifier. I tried to replace four sequential normal spaces by one hyphen character resulting in
'- - ' instead of '-'. I don't know why it works this way.
<?php
// Replace one or more horizontal white spaces
// of any kind by one normal space character
$str = preg_replace ('/\h+/u', ' ', $str);
?>