...

View Full Version : Matching "*" using preg_match. Please help.



ramki067
12-07-2010, 07:38 AM
HI,

I need to search for "*" in $value. The below code is not matching. Please help.



if(preg_match('/(*)/i',$value))
{
$fields.="";
echo "* is selected<br>";
}


Thanks,
Ramki

kbluhm
12-07-2010, 07:43 AM
See http://www.php.net/preg_quote for a list of characters that must be escaped, or "quoted", to be literally matched.

preg_match( '/(\*)/', $value )
If all you're doing is checking for asterisks, or an asterisk count, it'll be much more efficient to use strpos() or substr_count():


if ( FALSE !== strpos( $value, '*' ) )
{
echo 'found at least one asterisk';
}



$asterisk_count = substr_count( $value, '*' );

if ( $asterisk_count > 0 )
{
echo 'number of asterisks found: ' . $asterisk_count;
}

ramki067
12-07-2010, 09:32 AM
Thanks buddy... it solved the issue.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum