Page 1 of 1

get url and trim it based on 2 - 3 characters

Posted: Thu Nov 22, 2007 3:51 am
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.

Posted: Thu Nov 22, 2007 3:57 am
by aaronhall
A combination of ltrim() and explode() would do the trick

Posted: Thu Nov 22, 2007 4:08 am
by ianhull
Thanks aaron,

could you help with a sample?

Thanks in adavnce.

Posted: Thu Nov 22, 2007 4:13 am
by ianhull
Would this work?

Code: Select all

$id = $_SERVER["SCRIPT_NAME"];
	
	ltrim($id, 1);
	
	$id = explode('-', $id, 4);
	
	$id = str_replace(' ','',$id);

Posted: Thu Nov 22, 2007 4:29 am
by ianhull
Thanks aaron, i managed it with

Code: Select all

$id = $_SERVER["SCRIPT_NAME"];
	
	$id = explode('-', $id, 2);
	
	$id = str_replace('/', '', $id[0]);

Posted: Thu Nov 22, 2007 5:29 am
by aaronhall
Well played - here's a (nasty looking) one liner for your amusement

Code: Select all

list($id) = explode('-', ltrim($_SERVER["SCRIPT_NAME"], '/'));

Posted: Fri Nov 23, 2007 4:42 pm
by ianhull
haha, thanks mate

Posted: Fri Nov 23, 2007 8:02 pm
by s.dot

Code: Select all

$id = array_shift(explode('-', $script));
;d