0

How can I use strstr to match the content of a variable exactly instead of just containing?

For example:

  • www.example.com/greenapples
  • www.example.com/redapples
  • www.example.com/greenapplesandpears

If my URL is set as a variable $myurl and I use the following..

if (strstr($myurl, 'redapples')) {
echo 'This is red apples';
    }

Then it also applies to the other URLs as they also include the word apples. How can I be specific?

Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • Why aren't you using strops()? – Mark Baker Apr 12 '13 at 22:24
  • But if I used strpos wouldn't that also detect that the word apples is in all of the variables? – fightstarr20 Apr 12 '13 at 22:34
  • What's confusing is this: `match the content of a variable exactly instead of just containing` What does that mean?? to match exactly, use `==`. – sachleen Apr 12 '13 at 22:41
  • If you're checking for `apples`, then yes... it will find `apples` in all of them; if you're testing for `redapples`, then it will only find it in one of them... perhaps you need to include `/` in your test string – Mark Baker Apr 12 '13 at 22:44

2 Answers2

1

Ehmm, just compare?

if ('www.mydomain.com/greenapples' === $myurl) {
    echo 'green apples!';
}

update

Without further info, I'm not sure if this fits your question, but if you're only interested in the last part of the URL and take into account the possibility that the URL contains a query-string (e.g. ?foo=bar&bar=foo), try something like this:

// NOTE: $myurl should be INCLUDING 'http://'
$urlPath = parse_url($myurl, PHP_URL_PATH);

// split the elements of the URL 
$parts = explode('/', $urlPath);

// get the last 'element' of the path
$lastPart = end($parts);


switch($lastPart) {
    case 'greenapples':
        echo 'green!';
        break;

    case 'greenapplesandpears':
        echo 'green apples AND pears!';
        break;

    default:
        echo 'Unknown fruit family discovered!';

}

Documentation:

http://www.php.net/manual/en/function.parse-url.php

http://php.net/manual/en/function.end.php

http://php.net/manual/en/control-structures.switch.php

thaJeztah
  • 27,738
  • 9
  • 73
  • 92
  • The URLs listed are just examples, they are dynamically generated usually so I can't match the entire URL – fightstarr20 Apr 12 '13 at 22:34
  • 2
    You should better explain what you actually want to compare – Martin Prikryl Apr 12 '13 at 22:37
  • @fightstarr20: then you need try to rephrase your question. Matching whole strings should be done with === or preferably even `strcmp`, so to me it's a valid answer to your question as well :) – Evert Apr 12 '13 at 22:38
  • @Evert you're right, although I kinda expected this was not the answer OP was looking for. Probably a regexp will be required, but it really depends on **what** should match and what **not** (e.g. Should `/some/apples/and/pears/` match **_apples_** or **_pears_**?) – thaJeztah Apr 12 '13 at 22:41
1

I don't know about PHP but this you should be able to do by using some string operations. use substr("www.mydomain.com/greenapples",strripos("www.mydomain.com/greenapples", "/"));

strripos - returns last position of a substring, say its "/" in your case. substr - return the substring after the given position

Something like this you can try.

surender8388
  • 474
  • 3
  • 12