Home ›
Get rid of extra spaces using PHP's preg_replaceGet rid of extra spaces using PHP's preg_replace
Searched words:
preg replace multiple spaces with one
strip white space
remove extra blank spaces with Perl RegEx
Right there in the examples for the php function preg_replace was how to strip whitespace from a string:
<?php
$str = preg_replace('/\s\s+/', ' ', $str);
?>Ereg replace looks prettier:
<?php
$string = trim(ereg_replace(' +', ' ', $string));
?>However, Agaric has read in several places that preg_replace tends to be faster.
Resolution
More like this
- Get the lesser of two or more values with PHP
- Importing a large number of Taxonomy terms (Gene Ontology) regularly
- Get the directory name from a file path string in PHP
- E-mails being sent or nodes being saved by batch api breaking on a regular schedule just during cron runs
- Getting Views as Data with Drupal 6 Views 2
Comments
Be aware, however, that
Be aware, however, that ereg_replace is depreciated and is eliminated in php6:
Better to use preg_replace for forward-compatibility.
Post new comment