Jump to content

Remove the end of a string.


Paul EC1

Recommended Posts

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

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;

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.

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

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;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.