Forum Moderators: coopster

Message Too Old, No Replies

Checking For Consecutive Spacing Inside A String

         

trigoon

2:40 pm on Feb 28, 2009 (gmt 0)

10+ Year Member



Hello,

How would I go about checking if a string contains more than one consecutive space in it?

What I mean by consecutive is to check if there is, anywhere in the string more than one space after another. A single space is okay but anything more than one in a row is what I want to check for.

Thanks in advance!

trigoon

3:14 pm on Feb 28, 2009 (gmt 0)

10+ Year Member



Found a way to do it:

strpos($string, " ");

Basically I figure that if I just want to check if a string has two or mroe consecutive spaces strpos will find it as long as there are two.

Nutter

9:32 pm on Feb 28, 2009 (gmt 0)

10+ Year Member



What about something like

$myString=preg_replace('/ {2,}/m', ' ', $myString);

rob7591

11:43 pm on Feb 28, 2009 (gmt 0)

10+ Year Member



I think he's just trying to find the spaces, Nutter, and in that case his method is probably the most efficient.

Even if you're trying to replace the spaces, regular expressions generally take longer to execute, so there's no reason not to just use str_replace(' ', '', $string); # Note 2 spaces here, forum won't show them both.