Web Analytics Made Easy -
StatCounter How to use preg_match to match items that are like a varible value? - CodingForum

Announcement

Collapse
No announcement yet.

How to use preg_match to match items that are like a varible value?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Resolved How to use preg_match to match items that are like a varible value?

    Hi all,

    im trying to use preg_match with my search function at the moment without using preg_match it matches the search term if its entered exactly as it is in my feed but even if its spelt the same but the caps are not in the correct place it wont find anything

    so i need to use preg_match so that it pulls out all instances of the search value whether its entered in caps or not and if there is string around the text other than the text i want.

    hope this makes sence?

    here is my attempt but nothing is showing
    PHP Code:
    if (isset($_POST['Search'])){
    $test $_POST['Search'];
    }


    $feedurl "http://www.kernow-connect.com/feeds/play.xml";
                
    $xml=simplexml_load_file($feedurl);
                
                    foreach (
    $xml->merchant->prod as $item){

                        if (
    preg_match("/\$test\b/i",$item->text->name)){
                        list(
    $width$height) = getimagesize($item->uri->mImage);
                           
                                echo
    "
                                <div class='inditemheader'>
                                <a href='
    {$item->uri->awTrack}'>{$item->text->name}</a></div><br />
                    
                                <div class='inditemimg'>
                                <a href='
    {$item->uri->awTrack}'>
                                <img src='
    {$item->uri->mImage}'
                                alt='
    {$item->text->name}' "
                                
    .($width>$height?"width='120'":"height='100' ").
                                
    "border='0'
                                vspace='0' /></a>
                                </div>
                                
                                <div class='inditemprice'>
                                Buy Now For £
    {$item->price->buynow}
                                </div>"
    ;                              
                           }
                    } 
    this below works if its matched EXACTLY
    PHP Code:
    if (isset($_POST['Search'])){
    $test $_POST['Search'];
    }


    $feedurl "http://www.kernow-connect.com/feeds/play.xml";
                
    $xml=simplexml_load_file($feedurl);
                
                    foreach (
    $xml->merchant->prod as $item){

                        if (
    $item->text->name == $test){
                        list(
    $width$height) = getimagesize($item->uri->mImage);
                           
                                echo
    "
                                <div class='inditemheader'>
                                <a href='
    {$item->uri->awTrack}'>{$item->text->name}</a></div><br />
                    
                                <div class='inditemimg'>
                                <a href='
    {$item->uri->awTrack}'>
                                <img src='
    {$item->uri->mImage}'
                                alt='
    {$item->text->name}' "
                                
    .($width>$height?"width='120'":"height='100' ").
                                
    "border='0'
                                vspace='0' /></a>
                                </div>
                                
                                <div class='inditemprice'>
                                Buy Now For £
    {$item->price->buynow}
                                </div>"
    ;                              
                           }
                    } 
    any ideas on how i can get this working?

    cheers all
    Last edited by LJackson; Apr 1, 2009, 03:23 PM.

  • #2
    try changing your pattern to "/\b$test\b/i" (I think thats what you were trying to do anyway but it was missing the b after the first \)
    http://www.hazelryan.co.uk

    Comment


    • #3
      cheers mate that works brilliantly, thank you.

      i have no idea what that code means but its working

      cheers
      Luke

      Comment


      • #4
        The / are sort of 'separator marks'. The \b means 'word break' ie. scunthorpe wouldn't be a match for a certain 4 letter word which I'm sure the forum wont let me say... words within words wont be a match if you have \b at either end. The i means case insensitive.
        http://www.hazelryan.co.uk

        Comment


        • #5
          cool thanks for explaining

          Comment


          • #6
            Hi mate,

            just wondering if the preg_match can be used within a sql query?

            here is my code
            PHP Code:
            $sql "SELECT *
                    FROM product_database
                    WHERE preg_match("
            /\b$test\b/i",productTitle)"
            i want to find all items in the database where the productTitle is like the $test value

            but the above code does not work it doesnt like the /\'s

            any ideas
            cheers
            luke
            Last edited by LJackson; Apr 5, 2009, 09:38 AM.

            Comment


            • #7
              See http://dev.mysql.com/doc/refman/5.1/en/regexp.html
              Digitalocean Cloud Hosting (Referral link - get $10 free credit) Fameco

              Comment


              • #8
                Hi Mate,

                thanks for the link, i found that earlier but was unable to get anything working

                this looked "to me" to be what i wanted?
                PHP Code:
                $sql "SELECT * FROM my_table " .
                    
                "WHERE my_field REGEXP '" mysql_real_escape_string($match) . "'"
                but when i applied it to my scenario nothing happens, even if i type in an exact match for it to search for it finds nothing?

                here is my code
                PHP Code:
                $sql "SELECT * FROM product_database " .
                    
                "WHERE productTitle REGEXP '" mysql_real_escape_string($test) . "'"
                and i tried
                PHP Code:
                $sql "SELECT * FROM product_database WHERE productTitle REGEXP '[[:<:]]$test[[:>:]
                but still nothing, can you shed some light on this for me please!!

                thanks

                Comment


                • #9
                  ok i have managed to solve this but i have another problem which is related to this but i will start a new thread.

                  thanks for your help mate
                  mate

                  Comment

                  Working...
                  X