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 11 of 11
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation is it possible to check if part of a string matches a value in an array???

    Hi All

    i have a title
    e.g xbox 360 assassins creed: part 2

    and i am trying to find out if any part of that title matches an item in this array
    PHP Code:
    $platform_array = array("Xbox 360""PS3""Wii""DS""PSP""PS2"); 
    which it does, then i want a varible to be assigned the value which is matches
    e.g platform_result = xbox 360

    not im not sure if the space will cause any problems and the fact that it needs to be case incensitive

    here is the code i have tried
    PHP Code:
    $platform_array = array("Xbox 360""PS3""Wii""DS""PSP""PS2");
    foreach(
    $platform_array as $platform)
    {
        if(
    in_array($keywords,$platform))
        {
            
    $platform_result $platform;
        }
    }
    print 
    $platform_result
    but nothing prints out, maybe because its trying to match the entire title which it does not find?

    any help with this would be appreciated
    thanks
    Luke
    Last edited by LJackson; 02-04-2010 at 04:50 PM.

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Try this
    PHP Code:
    $platform_array = array('Xbox 360''PS3''Wii''DS''PSP''PS2');
    $text 'xbox 360 assassins creed: part 2';
    $match false;
    foreach(
    $platform_array as $v) {
        if(
    preg_match('/'.preg_quote($v).'/i'$text)) {
            
    $match $v;
            break;
        }
    }
    if(
    $match) {
        echo 
    $match;
    }else{
        echo 
    'No match found';

    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    LJackson (02-04-2010)

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    WOW thanks mate, works a treat !!!!

    cheers
    Luke

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    No problem
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    and how would one check to see if there is an ":" in the title or string?

    cheers
    Luke

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    as in the same method? you could add it to the array or just use strpos()
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    of course how thick of me lol

    cheers
    Luke

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok i am now trying to check if some text is present in a string and if it is remove it

    so i have this
    PHP Code:
                            switch($category['category'])
                            {
                                case 
    "Desktop Computers";
                                
    $brand_array = array("HP","Packard Bell","Packard Bl","Compaq","Advent","Centurion","imedia");
                                
    $match false;
                                foreach(
    $brand_array as $v
                                {
                                    if(
    preg_match('/'.preg_quote($v).'/i'$prodName)) 
                                    {
                                        
    $platform $v;
                                        
    $short_title preg_replace("/$platform/U"""$prodName);
                                        break;
                                    }
                                } 
    which does work but it seems to be case sensitive

    if i have a title COMPAQ 123THY LAPTOP using the above code would not remove COMPAQ because its Compaq in the array.

    how do i make it so that it doesnt matter what the case is?

    many thanks
    Luke

  • #9
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    change the U in the second regex to i. It should be used with preg_quote too like the original
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    LJackson (02-06-2010)

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Cheers mate!!! appreciate it

  • #11
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    one more question sorry :P

    i have this code from an old project
    PHP Code:
    $short_title preg_replace("/.{3}(inch).*/"""$short_title) . "\n"
    which searches for the word inch and deletes everything after including the word inch and 3 characters before it, as this is slightly different to the other examples how would i set this as case insensitive? i tried
    PHP Code:
    $short_title preg_replace("/.{3}(inch).*/"""$short_title) . "\n"
    but no luck?

    edit
    ====

    ok had the /i in the wrong place the / was already in the expression just needed to add the i like so
    PHP Code:
    $short_title preg_replace("/.{3}(inch).*/i"""$short_title) . "\n"
    cheers
    Luke


  •  

    Posting Permissions

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