By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,122 Members | 2,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,122 IT Pros & Developers. It's quick & easy.

eregi whitespace detection problem

P: n/a
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 ?
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Sat, 30 Oct 2004 15:53:11 +0200, "Frank" <fr***********@hotmail.com> wrote:
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)
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'.
also tried [:space:] and [:blank:] without result

Who can help this regular newbie expressing himself ?


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 / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2

P: n/a
Thanks Andy! You're a timesaver!

Frank

"Andy Hassall" <an**@andyh.co.uk> schreef in bericht
news:i7********************************@4ax.com...
On Sat, 30 Oct 2004 15:53:11 +0200, "Frank" <fr***********@hotmail.com>
wrote:
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)


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'.
also tried [:space:] and [:blank:] without result

Who can help this regular newbie expressing himself ?


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 / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.