Jump to content

Simple Question


stig1

Recommended Posts

Simple question, probably even more simple answer.

 

How can you check a string that is returned from a database that if it has more than 1 space between words, that it will then remove the extra spaces.

 

For example... Made from quality thermal paper treated on one side to produce an image from    heat.

 

Should be.. Made from quality thermal paper treated on one side to produce an image from heat.

 

Thanks in advance for your help.

Link to comment
https://forums.phpfreaks.com/topic/147697-simple-question/
Share on other sites

The simplest way:

 

<?php
$testString = 'Made from quality thermal paper treated on one side to produce an image from    heat.';
echo preg_replace('/ +/', ' ', $testString);
?>

 

If you don't mind it removing line breaks you can do \s instead of the space inside of the regex pattern (It's certainly more readable).

Link to comment
https://forums.phpfreaks.com/topic/147697-simple-question/#findComment-775279
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.