3

I have the following custom text and HTML:

$text = "<p>Hello World!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Javascript development&nbsp;PHP Developer&nbsp;</p>";

How can I change that text, using regular expressions, into the following:

"<p>Hello World!&nbsp;Javascript development&nbsp;PHP Developer</p>"

Code:

$text = "<p>Hello World!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Javascript development&nbsp;PHP Developer&nbsp;</p>";
$result = preg_replace("/(&nbsp;)+/", "&nbsp;", $text);
echo $result;
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Andreas Hunter
  • 4,504
  • 11
  • 65
  • 125

2 Answers2

2

Your input text contains spaces after most &nbsp; which your regex does not account for. You can use \s* to allow for zero or more spaces before and after each non-breakable space HTML entity.

$result = preg_replace('/(\s*&nbsp;\s*)+/', '&nbsp;', $text);
cfillion
  • 1,340
  • 11
  • 16
2

You may use

$find = ['/^((?:&nbsp;|\s)+)|(?1)$/', '/\s*&nbsp;(?:\s*&nbsp;)*\s*/', '/\s+/'];
$replace = ['', '&nbsp;',' '];
$text = preg_replace($find, $replace, trim($text));

See the PHP demo.

Details

  • '/^((?:&nbsp;|\s)+)|(?1)$/' => '' removes &nbsp; and whitespaces at the start/end of string ((?1) here just recurses (?:&nbsp;|\s)+ and is here to make the pattern a bit shorter)
  • '/\s*&nbsp;(?:\s*&nbsp;)*\s*/' => '&nbsp;' replaces all sequences of whitespace and &nbsp; with at least one &nbsp; with one &nbsp;
  • '/\s+/' => ' ' replaces 1+ whitespaces with 1 regular space.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563