|
Posted by Thomas Mlynarczyk on 05/14/07 20:45
> $match = '* Hello World';
> $test = '* Hello';
> if ( preg_match ( "/$match/", $test ) ) echo "Matched";
The * is a special character in regular expressions, but here you want to
use it as a normal one. So it must be escaped: '\* Hello World'. And I think
you mixed up the parameters. This should work:
$pattern = '* Hello';
$subject = '* Hello World';
if ( preg_match( '/' . preg_quote( $pattern, '/' ) . '/', $subject ) )
{
echo 'Matched';
}
The function preg_quote will escape all special characters, including the
delimiter (as specified by the second argument). If the pattern is not
variable, however, you can, of course, simply write
$pattern = '/\* Hello/';
$subject = '* Hello World';
if ( preg_match( $pattern, $subject ) )
{
echo 'Matched';
}
Greetings,
Thomas
Navigation:
[Reply to this message]
|