Page 1 of 1
String problem
Posted: Sat May 20, 2006 3:31 am
by leewad
Hi
How would I get the first and last value from an string?
I have a string with the following:
How can i seperate the string so it display sizes = 7-12 ?
Thanks
Posted: Sat May 20, 2006 3:44 am
by Charles256
off the top of my head use the implode function, where the seperator is a comma, then use array sort to get it in ascending order. then use count on the array to figure out how many elements and then last but not least display array_element[0] (the smallest size) and array_element[n-1] where n is the size of the array.pseudo code below..
Code: Select all
$array=implode(",",$my_String);
$count=count($array);
$array=sort($array);
$n=$count-1;
echo "$array[0] - $array[$n]";
something like that should do nicely.

Posted: Sat May 20, 2006 4:04 am
by timvw
With strpos you can determine the first and last occurence of ','. And with substr you can get substrings (parts) of a string...
Posted: Sat May 20, 2006 4:51 am
by leewad
Thanks but I used the follow:
Code: Select all
$sizes= "7,8,9,10.5,11,11.5,12";
$sizes=explode(",",$sizes);
echo $sizes[0]." - ".array_pop($sizes);
Posted: Sat May 20, 2006 5:39 am
by Oren
I would do it like this:
Code: Select all
$sizes = '7,8,9,10.5,11,11.5,12';
echo $sizes{0} . ' - ' . substr($sizes, -1, 1);
Posted: Sat May 20, 2006 5:45 am
by leewad
But that would only give you 7 - 2
Posted: Sat May 20, 2006 5:53 am
by Oren
Sorry, my mistake. Just do it with the array
