Jump to content

Strip formatting in PHP?!?


jonw118

Recommended Posts

I have a line of code that pulls text in from the database. The text that has been put into the database was inserted using HTML from an editor. So, when I pull it out, I want the html formatting to be stripped from it and only display the unformatted text.

 

Here's the line of code that pulls it from the db:

<p>'.preg_replace('/\s\S*$/i', '', substr($new_products['products_description'], 0, 90)).' ... </p>

 

Can anyone give some advise what I would insert in the line to stip the formatting?

 

Thanks!

Jon

Link to comment
https://forums.phpfreaks.com/topic/99983-strip-formatting-in-php/
Share on other sites

Ok, -thanks- and as you are about to tell I'm struggling, as I read it, the line should go

 

<?php
$text = '<p>'.preg_replace('/\s\S*$/i', '', substr($new_products['products_description'], 0, 90)).' ... </p>' echo strip_tags($text);
echo "\n";

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?> 

 

But... I'm obviously doing something wrong here. Thanks so much for the help...

If you still have this line:

 

$text = '<p>'.preg_replace('/\s\S*$/i', '', substr($new_products['products_description'], 0, 90)).' ... </p>' echo strip_tags($text);

 

Change it to:

$text = '<p>'. preg_replace('/\s\S*$/i', '', substr($new_products['products_description'], 0, 90)).' ... </p>';
echo strip_tags($text);

 

Yeah, I see what you did there and made sense, but produces same here. Here's what I think is enough relevant code. Thanks so much for your help:

 

<table border=0 cellspacing=0 cellpadding=0 nof=ly>
  <tr valign=top align=left>
   <td width=49 height=1><img src="./clearpixel.gif" width=49 height=1 border=0 alt=""></td>
   <td width=206 height=1><img src="./clearpixel.gif" width=206 height=1 border=0 alt=""></td>
  </tr>
  <tr valign=top align=left>
   <td colspan=2 rowspan=2 width=255>
    <table id="Table3" border=0 background="./productbck.gif" cellspacing=1 cellpadding=2>
     <tr height=255>
      <td width=249>
       <p>
        <table width="100%" border=0 cellspacing=0 cellpadding=0 nof=te>
         <tr>
          <td align="CENTER">
           <table id="Table2" border=0 cellspacing=0 cellpadding=2 width=224 height=215>
            <tr height=47>
             <td width=220>
              <p style="text-align: center;"><span style="font-family: Verdana,Tahoma,Arial,Helvetica,Sans-serif; font-size: 11px;">
               <table width="100%" border=0 cellspacing=0 cellpadding=0 nof=te>
                <tr>
                 <td align="CENTER">
                  <table width=219 border=0 cellspacing=0 cellpadding=0 nof=te>
                   <tr>
                    <td class="TextObject" style="font-family: Verdana,Tahoma,Arial,Helvetica,Sans-serif; font-size: 11px; color: rgb(255,255,255); font-weight: bold; text-decoration: none ;">
                     <p style="text-align: center;">'.$product_contents[1].' <br> '.$product_contents[2].' </p>
                    </td>
                   </tr>
                  </table>
                 </td>
                </tr>
               </table>
               </span></p>
             </td>
            </tr>
            <tr height=34>
             <td>
              <p>
               <table width=215 border=0 cellspacing=0 cellpadding=0 align=left nof=te>
                <tr>
                 <td class="Text" style="font-family: Verdana,Tahoma,Arial,Helvetica,Sans-serif; font-size: 11px; color: rgb(0,0,0); text-decoration: none ;">
                  

                  
[color=red]<?php
$text = '<p>'. preg_replace('/\s\S*$/i', '', substr($new_products['products_description'], 0, 90)).' ... </p>';
echo strip_tags($text);
echo "\n";

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?> [/color] 


                 
                 </td>
                </tr>
               </table>
              </p>
             </td>
            </tr>
            <tr height=62>
             <td>
              <p>
               <table width=216 border=0 cellspacing=0 cellpadding=0 align=left nof=te>
                <tr>
                 <td class="TextObject">
                  <p>'.$product_contents[0].' </p>
                 </td>
                </tr>
               </table>
              </p>
             </td>
            </tr>
            <tr height=55>
             <td>
              <p>
               <table width="100%" border=0 cellspacing=0 cellpadding=0 nof=te>
                <tr>
                 <td align="CENTER"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $listing['products_id']) . '"><img id="201" height=29 width=159 src="./viewproduct.gif" border=0 alt=""></a></td>
                </tr>
               </table>

No it is outside of any <?php ?> tags.

 

Here are all the tags on that page:

 

<?php echo $listing_split->display_count(TEXT_DISPLAY_NUMBER_OF_PRODUCTS); ?>

 

<?php echo TEXT_RESULT_PAGE . ' ' . $listing_split->display_links(MAX_DISPLAY_PAGE_LINKS, tep_get_all_get_params(array('page', 'info', 'x', 'y'))); ?>

 

<?php echo $listing_split->display_count(TEXT_DISPLAY_NUMBER_OF_PRODUCTS); ?>

 

<?php echo TEXT_RESULT_PAGE . ' ' . $listing_split->display_links(MAX_DISPLAY_PAGE_LINKS, tep_get_all_get_params(array('page', 'info', 'x', 'y'))); ?>

 

And at the very end of the page:

<?php

  }

?>

Here's the <?php tag it's within (sorry for the length of code):

 

<?php
  }

  $list_box_contents = array();

  for ($col=0, $n=sizeof($column_list); $col<$n; $col++) {
    switch ($column_list[$col]) {
      case 'PRODUCT_LIST_MODEL':
        $lc_text = TABLE_HEADING_MODEL;
        $lc_align = '';
        break;
      case 'PRODUCT_LIST_NAME':
        $lc_text = TABLE_HEADING_PRODUCTS;
        $lc_align = '';
        break;
      case 'PRODUCT_LIST_MANUFACTURER':
        $lc_text = TABLE_HEADING_MANUFACTURER;
        $lc_align = '';
        break;
      case 'PRODUCT_LIST_PRICE':
        $lc_text = TABLE_HEADING_PRICE;
        $lc_align = 'right';
        break;
      case 'PRODUCT_LIST_QUANTITY':
        $lc_text = TABLE_HEADING_QUANTITY;
        $lc_align = 'right';
        break;
      case 'PRODUCT_LIST_WEIGHT':
        $lc_text = TABLE_HEADING_WEIGHT;
        $lc_align = 'right';
        break;
      case 'PRODUCT_LIST_IMAGE':
        $lc_text = TABLE_HEADING_IMAGE;
        $lc_align = 'center';
        break;
      case 'PRODUCT_LIST_BUY_NOW':
        $lc_text = TABLE_HEADING_BUY_NOW;
        $lc_align = 'center';
        break;
    }

    if ( ($column_list[$col] != 'PRODUCT_LIST_BUY_NOW') && ($column_list[$col] != 'PRODUCT_LIST_IMAGE') ) {
      $lc_text = tep_create_sort_heading($HTTP_GET_VARS['sort'], $col+1, $lc_text);
    }

    $list_box_contents[0][] = array('align' => $lc_align,
                                    'params' => 'class="productListing-heading"',
                                    'text' => ' ' . $lc_text . ' ');
  }

  if ($listing_split->number_of_rows > 0) {
    $rows = 0;
    $column = 0;
    echo ' 

              <table cellspacing=0 cellpadding=0>
               <tr><td width=533 height=0></td></tr>
               <tr><td>
                    <table cellspacing=0 cellpadding=0 width=531 align=center>
                     <tr>

         ';

    $listing_query = tep_db_query($listing_split->sql_query);
    while ($listing = tep_db_fetch_array($listing_query)) {
      $product_contents = array();
      $rows++;
      if (($rows/2) == floor($rows/2)) {
        $list_box_contents[] = array('params' => 'class="productListing-even"');
      } else {
        $list_box_contents[] = array('params' => 'class="productListing-odd"');
      }

      $cur_row = sizeof($list_box_contents) - 1;

      for ($col=0, $n=sizeof($column_list); $col<$n; $col++) {
        $lc_align = '';

        switch ($column_list[$col]) {
          case 'PRODUCT_LIST_MODEL':
            $lc_align = '';
            $lc_text = ' ' . $listing['products_model'] . ' ';
            break;
          case 'PRODUCT_LIST_NAME':
            $lc_align = '';
            if (isset($HTTP_GET_VARS['manufacturers_id'])) {
              $lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a>';
            } else {
              $lc_text = '' . $listing['products_name'] . '';
            }
            break;
          case 'PRODUCT_LIST_MANUFACTURER':
            $lc_align = '';
            $lc_text = ' <a href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $listing['manufacturers_id']) . '">' . $listing['manufacturers_name'] . '</a> ';
            break;
          case 'PRODUCT_LIST_PRICE':
            $lc_align = 'right';
            if (tep_not_null($listing['specials_new_products_price'])) {
              $lc_text = '<span style="color:#7F7F7F;font-size:11px"><s>' .  $currencies->display_price($listing['products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) . '</s></span><br><span style="font-size:12px">' . $currencies->display_price($listing['specials_new_products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) . '</span>';
            } else {
              $lc_text = '<span style="font-size:9px">' . $currencies->display_price($listing['products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) . '</span>';
            }
            break;
          case 'PRODUCT_LIST_QUANTITY':
            $lc_align = 'right';
            $lc_text = ' ' . $listing['products_quantity'] . ' ';
            break;
          case 'PRODUCT_LIST_WEIGHT':
            $lc_align = 'right';
            $lc_text = ' ' . $listing['products_weight'] . ' ';
            break;
          case 'PRODUCT_LIST_IMAGE':
            $lc_align = 'center';
            if (isset($HTTP_GET_VARS['manufacturers_id'])) {
              $lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $listing['products_image'], $listing['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a>';
            } else {
              $lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $listing['products_image'], $listing['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a>';
            }
            break;
          case 'PRODUCT_LIST_BUY_NOW':
            $lc_align = 'center';
            $lc_text = '<a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) . 'action=buy_now&products_id=' . $listing['products_id']) . '">' . tep_image_button('button_buy_now.gif', IMAGE_BUTTON_BUY_NOW) . '</a> ';
            break;
        }

        $list_box_contents[$cur_row][] = array('align' => $lc_align,
                                               'params' => 'class="productListing-data"',
                                               'text'  => $lc_text);
        $product_contents[] = $lc_text;       
      }


      $product_query = tep_db_query("select products_description  from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$listing['products_id'] . "' and language_id = '" . (int)$languages_id . "'");
      $product = tep_db_fetch_array($product_query);
      $new_products['products_description'] = $product['products_description'];

      echo '


                         <td width=177 valign=top>



<table border=0 cellspacing=0 cellpadding=0 nof=ly>
  <tr valign=top align=left>
   <td width=49 height=1><img src="./clearpixel.gif" width=49 height=1 border=0 alt=""></td>
   <td width=206 height=1><img src="./clearpixel.gif" width=206 height=1 border=0 alt=""></td>
  </tr>
  <tr valign=top align=left>
   <td colspan=2 rowspan=2 width=255>
    <table id="Table3" border=0 background="./productbck.gif" cellspacing=1 cellpadding=2>
     <tr height=255>
      <td width=249>
       <p>
        <table width="100%" border=0 cellspacing=0 cellpadding=0 nof=te>
         <tr>
          <td align="CENTER">
           <table id="Table2" border=0 cellspacing=0 cellpadding=2 width=224 height=215>
            <tr height=47>
             <td width=220>
              <p style="text-align: center;"><span style="font-family: Verdana,Tahoma,Arial,Helvetica,Sans-serif; font-size: 11px;">
               <table width="100%" border=0 cellspacing=0 cellpadding=0 nof=te>
                <tr>
                 <td align="CENTER">
                  <table width=219 border=0 cellspacing=0 cellpadding=0 nof=te>
                   <tr>
                    <td class="TextObject" style="font-family: Verdana,Tahoma,Arial,Helvetica,Sans-serif; font-size: 11px; color: rgb(255,255,255); font-weight: bold; text-decoration: none ;">
                     <p style="text-align: center;">'.$product_contents[1].' <br> '.$product_contents[2].' </p>
                    </td>
                   </tr>
                  </table>
                 </td>
                </tr>
               </table>
               </span></p>
             </td>
            </tr>
            <tr height=34>
             <td>
              <p>
               <table width=215 border=0 cellspacing=0 cellpadding=0 align=left nof=te>
                <tr>
                 <td class="TextObject" style="font-family: Verdana,Tahoma,Arial,Helvetica,Sans-serif; font-size: 11px; color: rgb(0,0,0); text-decoration: none ;">
                  


                  
<?php
$text = '<p>'. preg_replace('/\s\S*$/i', '', substr($new_products['products_description'], 0, 90)).' ... </p>';
echo strip_tags($text);
echo "\n";

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>


                 
                 </td>
                </tr>
               </table>
              </p>
             </td>
            </tr>
            <tr height=62>
             <td>
              <p>
               <table width=216 border=0 cellspacing=0 cellpadding=0 align=left nof=te>
                <tr>
                 <td class="TextObject">
                  <p>'.$product_contents[0].' </p>
                 </td>
                </tr>
               </table>
              </p>
             </td>
            </tr>
            <tr height=55>
             <td>
              <p>
               <table width="100%" border=0 cellspacing=0 cellpadding=0 nof=te>
                <tr>
                 <td align="CENTER"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $listing['products_id']) . '"><img id="Picture201" height=29 width=159 src="./viewproduct.gif" border=0 alt=""></a></td>
                </tr>
               </table>
              </p>
             </td>
            </tr>
           </table>
          </td>
         </tr>
        </table>
       </p>
      </td>
     </tr>
    </table>
   </td>
  </tr>
  <tr valign=top align=left>
   <td width=0></td>
  </tr>
</table>



                    
                         </td>



      ';
      $column ++;
      if ($column >= 3) {
        $rows ++;
        $column = 0;
        echo '


                     </tr>
                     <tr><td colspan=5 height=5></td></tr>
                     <tr><td colspan=5 align=center></td></tr>
                     <tr>


             ';
      } else echo '<td><width=1></td>';
    
    
    
    }

    echo '


                        </tr>
                    </table>
               </td></tr>
               <tr><td></td></tr>
              </table>



         ';

    //new productListingBox($list_box_contents);
  } else {
    $list_box_contents = array();

    $list_box_contents[0] = array('params' => 'class="productListing-odd"');
    $list_box_contents[0][] = array('params' => 'class="productListing-data"',
                                   'text' => TEXT_NO_PRODUCTS);

    echo '<div class=PageHeading>Categories</div>';
    new productListingBox($list_box_contents);
  }

  if ( ($listing_split->number_of_rows > 0) && ((PREV_NEXT_BAR_LOCATION == '2') || (PREV_NEXT_BAR_LOCATION == '3')) ) {
?>

Yeah, remove the <?php tags surrounding it. You'll also need to end echo'ing all that html before it. Might look something like....

 

<td class="Text" style="font-family: Verdana,Tahoma,Arial,Helvetica,Sans-serif; font-size: 11px; color: rgb(0,0,0); text-decoration: none ;">';
$text = '<p>'. preg_replace('/\s\S*$/i', '', substr($new_products['products_description'], 0, 90)).' ... </p>';
echo strip_tags($text);
echo "\n";
echo strip_tags($text, '<p><a>');
echo '         </td>
               </tr>
              </table>

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.