Jeffro Posted June 13, 2011 Share Posted June 13, 2011 I need to search urls that I have contained in variables ($myurl) for specific expressions. The expressions might be in different places, as well. Any idea of how I could write the "if" statement to achieve this? $myurl = "http://mydomain.com/abc/moretext.html" // /abc/ is found, so condition is true or $myurl = "http://mydomain.com/stl/abc/moretext.html" // /abc/ is found, so condition is true or $myurl = "http://mydomain.com/xyz/blah.html" // /xyz/ is found, so condition is true or $myurl = "http://mydomain.com/efg/blah.html" // none of the below codes are found, so condition is false Here's the logic of what I'm trying to code. Any idea how to achieve this? if ($myurl contains /abc/ || $myurl contains /efg/ || $myurl contains /xyz/) { do this; } Quote Link to comment https://forums.phpfreaks.com/topic/239197-how-to-create-this-if-statement/ Share on other sites More sharing options...
devWhiz Posted June 13, 2011 Share Posted June 13, 2011 stristr Quote Link to comment https://forums.phpfreaks.com/topic/239197-how-to-create-this-if-statement/#findComment-1228914 Share on other sites More sharing options...
fugix Posted June 13, 2011 Share Posted June 13, 2011 http://us.php.net/manual/en/function.stristr.php i think that you meant, http://www.php.net/manual/en/function.stristr.php, which leads to a case insensitve strstr() function, which you can use to check if those characters are present in your url. You could also you regular expressions...something like $pattern = '/\/[a-c]+?\//'; $pattern2 = '/\/[e-g]+?\//'; $pattern3 = '/\/[x-z]+?\//'; if(preg_match($pattern, $url) || preg_match($pattern1, $url) || preg_match($pattern2, $url)) { //do as you wish } however like I said, you can use str_pos() and determine whether or not it returns true Quote Link to comment https://forums.phpfreaks.com/topic/239197-how-to-create-this-if-statement/#findComment-1228917 Share on other sites More sharing options...
devWhiz Posted June 13, 2011 Share Posted June 13, 2011 Yeah I caught that after awhile, my mistake $myurl = "http://mydomain.com/abc/moretext.html" if(stristr($myurl, '/abc/') || stristr($myurl, '/efg/') || stristr($myurl, '/xyz/')) { code to execute... } Quote Link to comment https://forums.phpfreaks.com/topic/239197-how-to-create-this-if-statement/#findComment-1228919 Share on other sites More sharing options...
Fadion Posted June 13, 2011 Share Posted June 13, 2011 As you only need to check if a string contains another string, stripos will be faster than stristr(). An example: <?php $myurl = 'http://mydomain.com/abc/moretext.html'; $search = 'abc'; if (stripos($myurl, $search) !== false) { echo 'This URL contains useful data.'; } ?> However, just a simple string search won't work very well in your case. Imagine having a url: "http://www.abcdomain.com/cde/sometext.html" and you're searching for "abc". In this case stripos() would return true because it finds the string (no matter where it is) and it may not be much of help. You could just search for with stripos() for "abc/" or better, use a code like follows: <?php $myurl = 'http://mydomain.com/abc/moretext.html'; $search = 'abc'; /* ** Used parse_url() here just to make the output cleaner. ** It's used to get the path (everything after the domain name), ** but it can be ommited and skipped directly to explode(). */ $path = parse_url($myurl, PHP_URL_PATH); $path = trim($path, '/'); $pieces = explode('/', $myurl); if (in_array($search, $pieces)) { echo 'This URL contains useful data.'; } ?> The code explodes the path to an array, where each individual path element is an array item. In this way you check for actual path data, not what a string contains. Hope it helps PS: Others have posted about stripos() while I was typing, but anyway. Quote Link to comment https://forums.phpfreaks.com/topic/239197-how-to-create-this-if-statement/#findComment-1228922 Share on other sites More sharing options...
Jeffro Posted June 13, 2011 Author Share Posted June 13, 2011 Thanks for all the great solutions. I'm going to investigate all the other responses now. I made the first one work and was getting ready to reply thanks to that when seeing all the rest. This appears to work well: if (stristr($myurl,"/abc/") || stristr($myurl,"/xyz/") || stristr($myurl,"/efg/") ) { echo "myurl is true"; } Quote Link to comment https://forums.phpfreaks.com/topic/239197-how-to-create-this-if-statement/#findComment-1228924 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.