Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #15896 > unrolled thread

find a space in a string, better fast solution?

Started byalbert <alstopspam@stopaspam.com>
First post2015-11-14 11:58 +0100
Last post2015-11-14 09:18 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  find a space in a string, better fast solution? albert <alstopspam@stopaspam.com> - 2015-11-14 11:58 +0100
    Re: find a space in a string, better fast solution? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-11-14 13:08 +0100
    Re: find a space in a string, better fast solution? Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-14 09:18 -0500

#15896 — find a space in a string, better fast solution?

Fromalbert <alstopspam@stopaspam.com>
Date2015-11-14 11:58 +0100
Subjectfind a space in a string, better fast solution?
Message-ID<n2744a$2v9$1@speranza.aioe.org>
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

[toc] | [next] | [standalone]


#15898

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-11-14 13:08 +0100
Message-ID<2535573.JF6dglbBag@PointedEars.de>
In reply to#15896
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.

[toc] | [prev] | [next] | [standalone]


#15901

FromJerry Stuckle <jstucklex@attglobal.net>
Date2015-11-14 09:18 -0500
Message-ID<n27fnd$ktr$1@dont-email.me>
In reply to#15896
On 11/14/2015 5:58 AM, albert wrote:
> 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
> 
> 

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
jstucklex@attglobal.net
==================

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web