Reply to Re: Regex with a '*' in it?

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация