Paul EC1 Posted July 9, 2008 Share Posted July 9, 2008 Hi All, I have a string from a basket that i need lose the end of , basically anything after and including “:Subtotal” needs to be dropped. the thing is !!!! “:Subtotal” can be much further along the string depending how many item are in the basket. the string always take a similar format - item : cost : qty : total e.g. 1 item Rosa Centre Mix :35.00 :1 :35.00 :Rose Vase :45.00 :1 :45.00 :Subtotal :80.00 :Delivery :0.00 :Total :80.00 : : : : : : : : : : : Or 2 items Rose Vase :45.00 :1 :45.00 :Subtotal :80.00 :Delivery :0.00 :Total :80.00 : : : : : : : : : : : I thought of something using this $i = str_replace('','',$i); Any help would be very much appreciated. Many thanks Paul Link to comment https://forums.phpfreaks.com/topic/113996-remove-the-end-of-a-string/ Share on other sites More sharing options...
Helmet Posted July 9, 2008 Share Posted July 9, 2008 $parts = explode(":Subtotal",$string); http://ca.php.net/explode Link to comment https://forums.phpfreaks.com/topic/113996-remove-the-end-of-a-string/#findComment-585891 Share on other sites More sharing options...
Paul EC1 Posted July 9, 2008 Author Share Posted July 9, 2008 Many thanks for your speedy responce, but am i missing somthing ? $oldstring = "Rosa Centre Mix :35.00 :1 :35.00 :Rose Vase :45.00 :1 :45.00 :Subtotal :80.00 :Delivery :0.00 :Total :80.00 : : : : : : : : : : :"; $newstring = explode(':', $oldstring, -15); $_SESSION["newstring"]=$newstring; Link to comment https://forums.phpfreaks.com/topic/113996-remove-the-end-of-a-string/#findComment-585901 Share on other sites More sharing options...
Helmet Posted July 9, 2008 Share Posted July 9, 2008 Yup It's as simple as $oldstring = "Rosa Centre Mix :35.00 :1 :35.00 :Rose Vase :45.00 :1 :45.00 :Subtotal :80.00 :Delivery :0.00 :Total :80.00 : : : : : : : : : : :"; $parts = explode(":Subtotal",$oldstring); will give you an array with two parts: $parts[0] which will be "Rosa Centre Mix :35.00 :1 :35.00 :Rose Vase :45.00 :1 :45.00 " and $parts[1] which will be " :80.00 :Delivery :0.00 :Total :80.00 : : : : : : : : : : :" explode used this way just uses ":Subtotal" as a delimiter to split the string into two parts. Sounds like $parts[0] is what you wanted. Link to comment https://forums.phpfreaks.com/topic/113996-remove-the-end-of-a-string/#findComment-585912 Share on other sites More sharing options...
kenrbnsn Posted July 10, 2008 Share Posted July 10, 2008 Or you can do: <?php $oldstring = "Rosa Centre Mix :35.00 :1 :35.00 :Rose Vase :45.00 :1 :45.00 :Subtotal :80.00 :Delivery :0.00 :Total :80.00 : : : : : : : : : : :"; list($newstring) = explode(":Subtotal",$oldstring); echo $newstring; ?> Ken Link to comment https://forums.phpfreaks.com/topic/113996-remove-the-end-of-a-string/#findComment-585939 Share on other sites More sharing options...
discomatt Posted July 10, 2008 Share Posted July 10, 2008 A better solution <?php $oldstring = "Rosa Centre Mix :35.00 :1 :35.00 :Rose Vase :45.00 :1 :45.00 :Subtotal :80.00 :Delivery :0.00 :Total :80.00 : : : : : : : : : : :"; $newstring = trim( strstr( $oldstring, ":Subtotal", 1 ) ); echo $newstring; ?> Link to comment https://forums.phpfreaks.com/topic/113996-remove-the-end-of-a-string/#findComment-585946 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.