Page 1 of 1

RegEx URL

Posted: Sun Feb 24, 2008 11:22 am
by Martinc126
Hello,

I need help with a little problem.

I'm wanting to only display a certain piece of information if the url matches.

For example if the url is http://www.mysite.com/news/news-112.html it will echo Correct

However if the url is http://www.mysite.com/news/cat-112.html it will echo incorrect.

The bit it picks up on will be the news- or cat-.

I've been told to use RegEx and URL but i can't find much information on how to create this.

Any examples or help would be really appreciated.

Thank You

Martin

Re: RegEx URL

Posted: Sun Feb 24, 2008 1:08 pm
by JAM
You could use parse_url() to get the part of the URL youre interested in, then do the if-then-else on that.

Re: RegEx URL

Posted: Sun Feb 24, 2008 1:49 pm
by Martinc126
JAM wrote:You could use parse_url() to get the part of the URL youre interested in, then do the if-then-else on that.
Thanks but it's not working like i need it to.

Basically i need it to

pick out .com/news/news- and echo correct.php

Pick out .com/news/cat- and echo wrong.php

The bit after news- and cat- will change depending on the page the visitor is view.

Martin

Re: RegEx URL

Posted: Mon Feb 25, 2008 10:55 am
by JAM
Tried using substr() and other functions to it? I wasn't trying to imply that parse_url() was the only function you needed.
Example;

Code: Select all

    $url = 'http://www.mysite.com/news/cat-112.html';
    $array = parse_url($url);
    echo substr($array['path'], 0, 10);
You don't even have to use parse_url() and so forth if that is to difficult or lacking usefullness to use in your case. Check ereg()/preg() and the section about regular expression in general. There is alot of examples in the comments.