Paul EC1 Posted July 5, 2008 Share Posted July 5, 2008 Hi all, Kinda new to this ??? so please be kind. Well i have a string ($trCart) and i need to remove the html and replace it with an : after each variable. Well, this is what i have so far. After we run :- echo strip_tags($strCart); this we get Name Price[£] Quantity Total[£] Mixed Roses 39.00 1 39.00 But i need my result to be Name Price[£] : Quantity Total[£] : Mixed Roses : 39.00 : 1 : 39.00 Any help would be very much appreciated at this late hour (GMT 00:17) Regards Paul Quote Link to comment https://forums.phpfreaks.com/topic/113389-replace-html-in-sting-with-an/ Share on other sites More sharing options...
abch624 Posted July 5, 2008 Share Posted July 5, 2008 try this mate <?php $trCartarray = explode('html ', $trCart); foreach ($trCartarray as $count){if ($count !== "") {$ref = $count.':'; echo $ref;}}?> Quote Link to comment https://forums.phpfreaks.com/topic/113389-replace-html-in-sting-with-an/#findComment-582579 Share on other sites More sharing options...
TransmogriBenno Posted July 6, 2008 Share Posted July 6, 2008 That code assumes that you're after a string, 'html', which isn't the case, or you wouldn't be using strip_tags. Your best bet is probably a regular expression hack, like this: $strCart = preg_replace ('/<[^>]*>/', ':', $strCart); You might need to escape the < and > in the regex, I can't remember. Quote Link to comment https://forums.phpfreaks.com/topic/113389-replace-html-in-sting-with-an/#findComment-582736 Share on other sites More sharing options...
sasa Posted July 6, 2008 Share Posted July 6, 2008 can you post example of $strCart Quote Link to comment https://forums.phpfreaks.com/topic/113389-replace-html-in-sting-with-an/#findComment-582791 Share on other sites More sharing options...
Paul EC1 Posted July 6, 2008 Author Share Posted July 6, 2008 Hi All, Many thanks for your responses, i have had a good night’s sleep now and will hopefully make more sense for you all. The unstrapped $strCart returns: * note this was got by right clicking and view source! <?php print_r($strCart); ?> <table id="basket" class="orderDeliveryTable" cellspacing="0"> <thead> <tr> <th> Name </th> <th class="price"> <em>Price</em><span>[£]</span> </th> <th class="quantity"> Quantity </th> <th class="priceTotal"> <em>Total</em><span>[£]</span> </th> </tr> </thead> <tbody><tr class="lL"> <td class="name"><a href="?p=productsMore&iProduct=84">Baby Basket Blue</a> </td> <td class="price"> 35.00 </td> <td class="quantity"> 1 </td> <td class="priceTotal"> 35.00 </td> </tr><tr class="tfoot" id="basketSummary"> <th colspan="3"> Subtotal: </th> <th class="priceTotal" id="orderCost"> 35.00 <script type="text/javascript"> <!-- var fOrderCost = "35.00"; //--> </script> </th> </tr><tr class="tfoot" id="courierSummary"> <th colspan="3"> Delivery Cost: </th> <th class="priceTotal" id="deliveryCost"> 0.00 </th> </tr> <tr class="tfoot" id="orderSummary"> <th colspan="3"> Total: </th> <th class="priceTotal" id="summaryCost"> 35.00 </th> </tr> <tr class="tfoot"> <td colspan="4"> The strapped $strCart returns: <?php echo strip_tags($strCart); echo "\n"; ?> Name Price[£] Quantity Total[£] Baby Basket Blue 35.00 1 35.00 Subtotal: 35.00 Delivery Cost: 0.00 Total: 35.00 The strapped $strCart with inserts returns <?php $strCart = preg_replace ('/<[^>]*>/', ':', $strCart); print_r($strCart); ?> : : : : Name : : :Price::[£]: : : Quantity : : :Total::[£]: : : : :: ::Baby Basket Blue: : : 35.00 : : 1 : : 35.00 : :: : Subtotal: : : 35.00 : : : : :: : Delivery Cost: : : 0.00 : : : : Total: : : 35.00 : : : : :: : : : : My desired result would be: <?php Some lovely code... ?> : Name : Price::[£] : Quantity : Total[£] :Baby Basket Blue : 35.00 : 1 : 35.00 : Subtotal : 35.00 : Delivery Cost : 0.00 : Total : 35.00 : Any and all help is very much appreciated.. Paul Quote Link to comment https://forums.phpfreaks.com/topic/113389-replace-html-in-sting-with-an/#findComment-582822 Share on other sites More sharing options...
aliento Posted July 6, 2008 Share Posted July 6, 2008 replace the space with : $strCart = str_replace(" "," : ",$strCart); if i understood well! Quote Link to comment https://forums.phpfreaks.com/topic/113389-replace-html-in-sting-with-an/#findComment-582835 Share on other sites More sharing options...
Paul EC1 Posted July 6, 2008 Author Share Posted July 6, 2008 Hi all, Ok we are getting a little closer If i run, <?php $i = strip_tags($strCart); $i = str_replace(" "," : ",$i); echo $i; ?> The result is: (not good) : : : : : : : : : : : : : Name : : : : : : : : Price[£] : : : : : : : : Quantity : : : : : : : : Total[£] : : : : : : : : : : : : : : : Baby : Basket : Blue : : : : : : : : 35.00 : : : : : : : : 1 : : : : : : : : 35.00 : : : : : : : : : : : : Subtotal: : : : : : : : : 35.00 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : Delivery : Cost: : : : : : : : : 0.00 : : : : : : : : : : : : : : : : Total: : : : : : : : : 35.00 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : My be all it needs now is to replace : > 1 with “ “ To give us e.g “Name : Price[£]” and not “Name : : : : : : : : Price[£]”. Any ideas how this can i do this please. Regards Paul Quote Link to comment https://forums.phpfreaks.com/topic/113389-replace-html-in-sting-with-an/#findComment-582854 Share on other sites More sharing options...
wildteen88 Posted July 6, 2008 Share Posted July 6, 2008 Use preg_replace maybe: $i = preg_replace("/\s+/", " : ",$i); Quote Link to comment https://forums.phpfreaks.com/topic/113389-replace-html-in-sting-with-an/#findComment-582866 Share on other sites More sharing options...
aliento Posted July 6, 2008 Share Posted July 6, 2008 Its simple. You wrote at the $strCart not one space but a lot between Name Price etc . PHP cant replace an unknown number of character set so you should go back to code and correct the $strCart to have one space. A stupid idea is : for($i=0;$i<count($strCart);$i++) $strCart[$i]; // the $strCart[$i] contains each character of the string with an if for every character and a counter you can delete the unwonted characters and replace them with one : I am too lazy and i cant write the all code Maybe somebody have a better idea ! ... Quote Link to comment https://forums.phpfreaks.com/topic/113389-replace-html-in-sting-with-an/#findComment-582871 Share on other sites More sharing options...
kenrbnsn Posted July 6, 2008 Share Posted July 6, 2008 Here's what I came up with: <?php $str = ': : : : : : : : : : : : : Name : : : : : : : : Price[£] : : : : : : : : Quantity : : : : : : : : Total[£] : : : : : : : : : : : : : : : Baby : Basket : Blue : : : : : : : : 35.00 : : : : : : : : 1 : : : : : : : : 35.00 : : : : : : : : : : : : Subtotal: : : : : : : : : 35.00 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : Delivery : Cost: : : : : : : : : 0.00 : : : : : : : : : : : : : : : : Total: : : : : : : : : 35.00 : : : : : : : : : : : : : : : : : : : : : : : : : : : : :'; $new_str = preg_replace('[\: *\: ]',':',$str); echo '<pre>' . $new_str . '</pre>'; $new_str = preg_replace('[\:*\:]',':',$new_str); echo '<pre>' . $new_str . '</pre>'; $new_str = trim(str_replace(':',' : ',str_replace(' ','',$new_str)),' : '); echo '<pre>' . $new_str . '</pre>'; ?> Someone who knows regular expressions better than I do might do much better. Ken Quote Link to comment https://forums.phpfreaks.com/topic/113389-replace-html-in-sting-with-an/#findComment-582873 Share on other sites More sharing options...
Paul EC1 Posted July 6, 2008 Author Share Posted July 6, 2008 And the winner is wildteen88, in first place with a gold star simple but effective. <?php $i = strip_tags($strCart); $i = preg_replace("/\s+/", " : ",$i); echo $i; ?> Now returns : Name : Price[£] : Quantity : Total[£] : Baby : Basket : Blue : 35.00 : 1 : 35.00 : Subtotal: : 35.00 : Delivery : Cost: : 0.00 : Total: : 35.00 : We still have a couple of : : eg after Subtotal: :, but i am sure i can figure that out myself. Again many, many thanks for the help on this, one and all. Paul Quote Link to comment https://forums.phpfreaks.com/topic/113389-replace-html-in-sting-with-an/#findComment-582886 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.