Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 17th, 2005, 10:22 AM
Frank
Guest
 
Posts: n/a
Default eregi whitespace detection problem

I'm having trouble detecting whitespaces in strings.

Set up this test:

echo "<br>example 1:".intval(eregi("^\s","teststring"));
echo "<br>example 2:".intval(eregi("^\s","test string"));

Both resulting in 0 (zero)

also tried [:space:] and [:blank:] without result

Who can help this regular newbie expressing himself ?


  #2  
Old July 17th, 2005, 10:22 AM
Andy Hassall
Guest
 
Posts: n/a
Default Re: eregi whitespace detection problem

On Sat, 30 Oct 2004 15:53:11 +0200, "Frank" <frankdegrauwe@hotmail.com> wrote:
[color=blue]
>I'm having trouble detecting whitespaces in strings.
>
>Set up this test:
>
>echo "<br>example 1:".intval(eregi("^\s","teststring"));
>echo "<br>example 2:".intval(eregi("^\s","test string"));
>
>Both resulting in 0 (zero)[/color]

Which is correct. Your pattern is looking for:

^ - start of line
\s - followed by the literal string 's'

Consider:

echo "<br>example 3:".intval(eregi("^\s","string test"));

Output:

example 3:1

Because that does start with an 's'.
[color=blue]
>also tried [:space:] and [:blank:] without result
>
>Who can help this regular newbie expressing himself ?[/color]

For starters don't use ereg, use the preg functions, they're better for
numerous reasons.

http://uk.php.net/manual/en/ref.pcre.php

It looks like you're already trying to use Perl-compatible expressions, since
you used \s. This would work if you used preg_match, as \s means whitespace in
Perl-compatible expressions, but not in POSIX expressions (as used by ereg).
You then just need to remove the leading ^.

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
  #3  
Old July 17th, 2005, 10:22 AM
Frank
Guest
 
Posts: n/a
Default Re: eregi whitespace detection problem

Thanks Andy! You're a timesaver!

Frank

"Andy Hassall" <andy@andyh.co.uk> schreef in bericht
news:i797o0hi5itqt2id6ku6nr5c4v4vae8g4p@4ax.com...[color=blue]
> On Sat, 30 Oct 2004 15:53:11 +0200, "Frank" <frankdegrauwe@hotmail.com>
> wrote:
>[color=green]
>>I'm having trouble detecting whitespaces in strings.
>>
>>Set up this test:
>>
>>echo "<br>example 1:".intval(eregi("^\s","teststring"));
>>echo "<br>example 2:".intval(eregi("^\s","test string"));
>>
>>Both resulting in 0 (zero)[/color]
>
> Which is correct. Your pattern is looking for:
>
> ^ - start of line
> \s - followed by the literal string 's'
>
> Consider:
>
> echo "<br>example 3:".intval(eregi("^\s","string test"));
>
> Output:
>
> example 3:1
>
> Because that does start with an 's'.
>[color=green]
>>also tried [:space:] and [:blank:] without result
>>
>>Who can help this regular newbie expressing himself ?[/color]
>
> For starters don't use ereg, use the preg functions, they're better for
> numerous reasons.
>
> http://uk.php.net/manual/en/ref.pcre.php
>
> It looks like you're already trying to use Perl-compatible expressions,
> since
> you used \s. This would work if you used preg_match, as \s means
> whitespace in
> Perl-compatible expressions, but not in POSIX expressions (as used by
> ereg).
> You then just need to remove the leading ^.
>
> --
> Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool[/color]


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles