SitePoint Sponsor |
|
User Tag List
Results 1 to 17 of 17
Thread: Analysing substrings.
-
Jun 14, 2007, 12:40 #1
- 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'; } }
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.
-
Jun 14, 2007, 12:53 #2
- 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.
-
Jun 14, 2007, 13:00 #3
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
-
Jun 14, 2007, 13:01 #4
- 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!
-
Jun 14, 2007, 13:04 #5
- 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!
-
Jun 14, 2007, 13:12 #6
- 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.
-
Jun 14, 2007, 13:45 #7
- 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; }
-
Jun 14, 2007, 13:57 #8
- 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]; }
-
Jun 14, 2007, 14:08 #9
- Join Date
- Nov 2006
- Posts
- 71
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Excellent short function, thanks kromey! I appreciate it.
-
Jun 14, 2007, 14:10 #10
- 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 ) > 1 ) { 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 */
Computers and Fire ...
In the hands of the inexperienced or uneducated,
the results can be disastrous.
While the professional can tame, master even conquer.
-
Jun 14, 2007, 14:13 #11
- 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.
-
Jun 14, 2007, 14:38 #12
- 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.
-
Jun 14, 2007, 14:51 #13
-
Jun 14, 2007, 14:57 #14
- 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?
-
Jun 14, 2007, 15:02 #15
- 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.
-
Jun 14, 2007, 15:11 #16
- 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.
-
Jun 14, 2007, 15:13 #17
- 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