SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Analysing substrings.

    Hello all,

    I'm writing a function that is passed a string, and the string is to be analysed for substrings. The substrings will determine the value of certain variables.

    With IF statements, it would look something like this:

    Code:
    myFunction($url) {
    
       // Check to see if the substring 'google.com' exists in $url.
       if (stripos($url, 'google.com') !== 0) {
          $sitename = 'Google';
          }
    
       }
    This is for a website I am developing that deals with various different affiliates, so there will be lots of different substrings to check (one for each affiliate partner) to determine $sitename.

    My question is, how would I accomplish the same thing with a SWITCH statement (checking $url for many different substrings and giving $sitename a different value based on whether or not the substring is found).

    Thanks in advance!

    P.S. If this looks trivial, I'm not actually using $sitename in the actual program, I just used it hypothetically because it's easier to describe than the actual process my function accomplishes, and if I learn how to accomplish the above process with a SWITCH statement, my question is answered and I can integrate it into my program.

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Short answer: Can't be done.

    Long answer: Can't be done. At least not in this fashion. If your strings are always in the same format and thus your target substring is always in the same place, you could extract the substring and then use a switch statement to figure out what processing your function needs to do. If this is the case, we need to know the format of your string.
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Not exactly sure if it's what you need, but you can use array_filter:

    Code php:
    function findnames($var){
      global $string;
      return stripos($string, $var)!==false;
    }
     
    $string="google.com yahoo.com";
     
    $sites=array("Google"=>"google.com","Yahoo!"=>"yahoo.com","MSN"=>"msn.com");
     
    $found=array_filter($sites, "findnames");
     
    print_r($found);
    Saul

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Didn't think so... I spent about 20 minutes looking it up but to no avail so I thought I'd try asking the experts.

    Since each URL is likely to have a different length of characters and will have various other substrings after the domain portion of the URL, there is no set pattern to the strings that are being analysed. I guess I'll have to use IF statements.

    Thanks for the reply!

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks php_daemon, I'll see if I can apply this to my code! Much appreciated!

  6. #6
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Variable-length domain names is no problem ("Behold! The power of REGEX!"). Provided we know what comes before (e.g. "http://") and what comes after (maybe "/"?), we can extract the domain name with no problem. If php_daemon's suggestion doesn't work for you (and I can't see any reason it won't), post a couple of example strings and we'll find your REGEX.
    PHP questions? RTFM
    MySQL questions? RTFM

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the ideas all. Threw this together and it works a charm:

    Code:
    function myFunction($url) {
    
       // Check URL string for 'www.'
       $www_check = stripos($url, 'www.');
    
       if ($www_check === FALSE) {
          $domain_start = 7;
          } else { $domain_start = 11; }
    
       // Cut off first part of URL (i.e. 'http://' or 'http://www.' depending on URL).
       $url_trimmed = substr($url, $domain_start);
    
       // Determine where the domain portion of URL ends.
       $domain_end = stripos($url_trimmed, '/');
    
       // Remove end portion of URL leaving just the domain.
       $domain = substr($url_trimmed, 0, $domain_end);
    
       switch ($domain) {
    
          case 'google.com':
             $sitename = 'Google';
             break;
    
          case 'yahoo.com':
             $sitename = 'Yahoo';
             break;
    
          }
    Any ideas on how to optimize the code, or is this coded to a good standard?

  8. #8
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    function getDomain($url)
    {
        $pattern = "#http://(www\.)?(.*?)/#i";
        preg_match($pattern, $url, $matches);
        return $matches[2];
    }
    This assumes that the domain is always prefixed with http://, and always ends with /. It will ignore the www. if present.
    PHP questions? RTFM
    MySQL questions? RTFM

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent short function, thanks kromey! I appreciate it.

  10. #10
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lets take the simple approach to domain names:

    PHP Code:
    $data explode"//"$url // Lets take the "http//" off of this url
    if ( count$data ) > ) { Is there a http in the first array?
        
    $parts explode"/" $data[1] ) // Strip off all the subdirectories
    } else {
        
    $parts explode"/" $data[0] ) // Strip off all the subdirectories
    }

    list( 
    $prefix$root$suffix ) = explode"."$parts[0]);

    echo 
    "The domain name is : $root"// Here is your domain name
    /* Now that you have a domain name you can run it through a select statement */ 
    You can take take the $root and compare it to an array of affiliate url root names. If it matches one then send the name to your select statement.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  11. #11
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What happens if you have a url like http://my.earthlink.net?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  12. #12
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the input Byron3!

    When dealing with a URL like http://my.earthlink.net, kromey's function outputs "my.earthlink.net" as the domain, while yours correctly outputs "earthlink.net".

    However, with URLs with no "www." or subdomain (i.e. "http://blahblahblah.com"), you would have to display $prefix, $root and $suffix to get the desired result in your function.

    Both functions could be tweaked. If I learn regular expressions, I'm sure subdomain prefixes could be handled with kromey's function, and with an IF statement comparing $suffix to the string "www." could get around the problem in yours... I think!

    Thanks again to both of you, if you have any further suggestions/solutions that would be great.

  13. #13
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you want subdomains stripped off as well? I'll play with my regex pattern, I'm sure I can make it do what you want, it'll just take a little tinkering.
    PHP questions? RTFM
    MySQL questions? RTFM

  14. #14
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To be honest, I think I could get away with having subdomain prefixes in the URLs, since it will probably be uniform across the whole affiliate's website in terms of product pages and all I'd have to do is include the subdomain prefix in the switch statement case.

    However, I'd be interested to see the code of how it would work removing the subdomain as well, just out of curiosity. Any recommended reading on regular expressions? Or should I just go RTFM?

  15. #15
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the pattern that will strip off subdomains:
    #http://(.*?\.)?(.*?\..*?)/#i
    Simply replace the pattern in my function above with this one and watch the joy.

    I started with the syntax reference in the PHP manual, but I quickly picked up a couple books. O'Reilly has some great ones on this subject, with their Pocket Reference being my favorite.
    PHP questions? RTFM
    MySQL questions? RTFM

  16. #16
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ( $prefix != "www" ) { $prefix "www"; }
    $sanatized_url $prefix ."."$root ."."$suffix
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  17. #17
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks both of you, I appreciate the help a lot.


Bookmarks

Posting Permissions

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