RegEx URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Martinc126
Forum Newbie
Posts: 2
Joined: Sun Feb 24, 2008 11:18 am

RegEx URL

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: RegEx URL

Post 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.
Martinc126
Forum Newbie
Posts: 2
Joined: Sun Feb 24, 2008 11:18 am

Re: RegEx URL

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: RegEx URL

Post 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.
Post Reply