Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

find a space in a string, better fast solution?

8 views
Skip to first unread message

albert

unread,
Nov 14, 2015, 5:58:27 AM11/14/15
to
If I have this
xx/yy/178 234 38
the space are only between the numbers (if there are number)

which is the most fast solution for know if after first number there is
a space?

1 Find the space for all the string ?
2 find the space after last occurency / ?



I need to do this:
I can have this
xx/yy/178 234 38 I have 2 spaces: do nothing
xx/yy/200 No spaces: take the value 200
xx/yy/200 45 I have 1 space: do nothing
xx/yy/ I have 1 space: do nothing because haven't the number

If in the string I have one space I do nothing
If I haven't space I need take first(and the unique) number


Thomas 'PointedEars' Lahn

unread,
Nov 14, 2015, 7:09:07 AM11/14/15
to
albert wrote:

> which is the most fast solution for know if after first number there is
> a space?

Pattern matching: <http://php.net/preg_match>

If the function returns 0 for the proper pattern, there either is no number
or there is no number followed by space.

If it returns 1 then, there is a number followed by space.

For the first number, all preceding characters (in the line) must be non-
digits. That can either be addressed with a negating *character class* (see
the corresponding section in the PHP manual) or with negative *lookbehind*
(see the section “Assertions”).

--
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

Jerry Stuckle

unread,
Nov 14, 2015, 9:18:38 AM11/14/15
to
OK, you could use several different ways (again, speed is not as
important as readability and maintainability). Again, a regex is
overkill for something so simple.

So it looks like you're only concerned of the times when you have no
spaces in your string, but have one value.

First thing I would do is get rid of any leading and trailing white
space using trim:

$str1 = trim($str);

Then I would use the previous examples to extract the numbers and see
how many spaces (and numbers) you have.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================
0 new messages