home

Quickies, by Andrea Olivato

text

Regex to change any BR in space in #PHP

This is a simple example showing how to change any <br> contained in an HTML string into a space, avoiding double spaces.

<?php
$string = 'Lorem <br>ipsum<br style="border:1px solid #CCC" />dolor<br /> sit<br style="height:1000px;">amet';

/* Stripping all other tags
 * [optional, depends on what you want to do 
 */
$string = strip_tags($string,'<br>');

/* Changing all <br> types into spaces */
$string = preg_replace('/<br[^>]*>/',' ',$string);

/* Avoiding double spaces */
$string = preg_replace('/[\ ]+/',' ',$string);

echo $string;

/* Will output: 'Lorem ipsum dolor sit amet' */

?>

2 years ago

October 14, 2009
Comments (View)
blog comments powered by Disqus