Jump to content

Replace Html in sting with an ":"


Paul EC1

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/113389-replace-html-in-sting-with-an/
Share on other sites

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.

Hi All,

 

Many thanks for your responses, i have had a good night’s sleep ;D 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

 

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

 

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 ! ...

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

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

 

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.