The following code outputs nothing, and after reading all the docs I
can still don't understand why.
Can anyone explain why word boundaries before the $ don't seem to be
matching?
Cheers,
Jo
<?php
$applePrice = '$5 per bunch';
$orangePrice = 'One bag $2';
$pattern = '/\b\\$/is';
if (preg_match($pattern, $applePrice))
{
echo "Apple Match!\n";
}
if (preg_match($pattern, $orangePrice))
{
echo "Orange Match!\n";
}
?>
I'd say the blank space is not a word boundary. Perhaps you mean:
$pattern = '/\b \\$/is';
--
-- http://alvaro.es - �lvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programaci�n web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
> $pattern = '/\b\\$/is';
This pattern looks for a word boundary followed by one backslash that
are at the end.
> if (preg_match($pattern, $applePrice))
> {
> echo "Apple Match!\n";
> }
To match a word (something between word boundaries) you may use this.
$pattern = '/\b.*?\b/is';
The question mark after the asterisk tells the search shall not be
greedy. It will consume the minimum of symbols to match the rule not as
many as possible.
You could even match a dollar $ followed by at least one digit:
$pattern = '/\$\d+/is';
--
http://portal.richler.de/ Namensportal zu Richler
http://www.richler.de/ Heiko Richler: Computer - Know How!
http://www.richler.info/ private Homepage
> Jo Jo S wrote:
>> $applePrice = '$5 per bunch';
>
>> $pattern = '/\b\\$/is';
The problem is most likely with what the regex engine considers a
word. The assertion likely failed because "$" is not considered
part of a word. You might consider rethinking your approach to
something like this.
$pattern = '/(?:^|\W+)\$/';
Also, notice I dropped the `/i' and `/s' modifiers, as they serve
no purpose at all in your pattern.
> This pattern looks for a word boundary followed by one backslash
> that are at the end.
No, that's not the case. The escaped backslash is escaped for
PHP, not the regex engine. Although the extra escape for PHP is
not necessary here, it is *not* incorrect.
$php_str = '/\\$/';
Regex engine sees:
/\$/
Situations where you need to be aware of both PHP and regex
escaping can get tricky.
<snip>
--
Curtis Dyer
<? $x='<? $x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>