Jump to content

How to create this "if" statement?


Jeffro

Recommended Posts

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;
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.