Forum Moderators: coopster

Message Too Old, No Replies

If URL Contains Then

         

steww

8:53 pm on Sep 15, 2011 (gmt 0)

10+ Year Member



Hi there wondering if anyone can help,

I'm trying to check if the current URL contains a set of characters and if so echo a div.

So far I have the following working:

<?php if (stripos($_SERVER['REQUEST_URI'],'/blue-widgets/') !== false)

{echo "<div></div>" ;}

?>


But that code is checking for an absolute match for "/blue-widgets/" I think, I'd like to be able to echo the div if the URL contains just the text "-widgets", therefore it would appear if the url contained "red-widgets" or "yellow-widgets" and so on.

Anyone know if this is possible?

Many Thanks

penders

10:29 pm on Sep 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That code checks for the string "/blue-widgets/" anywhere in the REQUEST_URI, so to check for "-widgets", just change it to "-widgets"!?

steww

10:48 pm on Sep 15, 2011 (gmt 0)

10+ Year Member



Ah thats a bit embarrassing! Im sure I tried it earlier and didn't work!

Worked great.

Is there also a way to say "-widgets" OR "-widget"?

penders

11:32 pm on Sep 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



:)

To check for "-widgets" OR "-widget" you're better off using preg_match() [uk3.php.net] and a regular expression (regex), like....

if (preg_match('@-widgets?@i',$_SERVER['REQUEST_URI'])) {


@ - marks the start and end of the regex. (regex delimiters)
i - after the regex makes it case-insensitive (like the i in stripos())
? - makes the preceeding 's' optional, so "-widgets" OR "-widget"
All other characters are matched literally.

lostdreamer

8:19 am on Sep 16, 2011 (gmt 0)

10+ Year Member



eehmm... D'oh!
The word "widgets" contains the word "widget", so you dont need to check for both, just for "-widget"


<?php if (stripos($_SERVER['REQUEST_URI'],'-widget/') !== false)

penders

10:22 am on Sep 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



D'oh! lol! Glad one of us is on the ball! :)