Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Matching "*" using preg_match. Please help.

    HI,

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

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

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    See http://www.php.net/preg_quote for a list of characters that must be escaped, or "quoted", to be literally matched.
    PHP Code:
    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():
    PHP Code:
    if ( FALSE !== strpos$value'*' ) )
    {
        echo 
    'found at least one asterisk';

    PHP Code:
    $asterisk_count substr_count$value'*' );

    if ( 
    $asterisk_count )
    {
        echo 
    'number of asterisks found: ' $asterisk_count;

    Last edited by kbluhm; 12-07-2010 at 01:53 PM.

  • Users who have thanked kbluhm for this post:

    ramki067 (12-07-2010)

  • #3
    New Coder
    Join Date
    Oct 2007
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks buddy... it solved the issue.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •