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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    Scotland
    Posts
    407
    Thanks
    0
    Thanked 0 Times in 0 Posts

    detecting phrase in a string

    am going to kill 2 birds with one stone here...

    what PHP command could I use to detect a certain string inside another string?

    example 1:
    $browser = $_SERVER['USER_AGENT'];

    how could i detect if $browser contains "MSIE"

    example 2:
    along the same lines but for detecting a key word or phrase in a paragraph
    My body's a temple... and like those ancient Greek ones it's a ruin
    sweenster.co.uk

  • #2
    New Coder
    Join Date
    Jun 2004
    Location
    Newcastle, England
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    http://www.php.net/manual/en/function.strpos.php

    either strpos or strripos for case insensitive search.
    Beauty is in the eye of the beer-holder

    Man who smoke pot, choke on handle

  • #3
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Or...

    Code:
    if(preg_match("/MSIE/i",$_SERVER['USER_AGENT']) {
         print "match";
    }
    else {
         print "no match";
    }

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    ... though mostly , if you can avoid regex do so (not just cos its harder ) but as its much slower than normal string operations , if you have to use regex , use the preg_$func functions as per Bcarl's post as they are much faster than ereg() etc

    but for a simple check like above (see MrSheds link)...

    PHP Code:
    <?
    if( strpos$_SERVER['USER_AGENT'] , 'MSIE' ) !== false ){
     echo 
    'IE';
    }
    ?>
    even though you could beef up the regex to check for several user-agents at once & then switch that result .. still in this case multiple strpos()'s will still be faster.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

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