get url and trim it based on 2 - 3 characters

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
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

get url and trim it based on 2 - 3 characters

Post by ianhull »

Hi guys,

I would like to get the

Code: Select all

_SERVER["SCRIPT_NAME"]
which would look like this

/68-vehichle-for-sale-MAKE-MODEL-PRICE.php

or after the records reach three characters

/268-vehichle-for-sale-MAKE-MODEL-PRICE.php

the record id for the page the number at the beginning of the page name.

I would like to somehow get this number and query the database for the results.

I know this is possible, but how?

I just need the number the rest is fine, im just wondering how to do it for two and three characters.


Thank in advance.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

A combination of ltrim() and explode() would do the trick
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Thanks aaron,

could you help with a sample?

Thanks in adavnce.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Would this work?

Code: Select all

$id = $_SERVER["SCRIPT_NAME"];
	
	ltrim($id, 1);
	
	$id = explode('-', $id, 4);
	
	$id = str_replace(' ','',$id);
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Thanks aaron, i managed it with

Code: Select all

$id = $_SERVER["SCRIPT_NAME"];
	
	$id = explode('-', $id, 2);
	
	$id = str_replace('/', '', $id[0]);
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Well played - here's a (nasty looking) one liner for your amusement

Code: Select all

list($id) = explode('-', ltrim($_SERVER["SCRIPT_NAME"], '/'));
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

haha, thanks mate
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

$id = array_shift(explode('-', $script));
;d
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply