Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all html tags from php string

Tags:

php

I want to display the first 110 characters of a database entry. Pretty easy so far:

<?php echo substr($row_get_Business['business_description'],0,110) . "..."; ?> 

But the above entry has html code in it that's been entered by the client. So it displays:

<p class="Body1"><strong><span style="text-decoration: underline;">Ref no:</span></strong> 30001<strong></stro... 

Obviously no good.

I just want to strip out all html code, so I need to remove everything between < and > from the db entry THEN display the first 100 chars.

Any ideas anyone?

like image 359
jimbeeer Avatar asked Feb 04 '13 09:02

jimbeeer


People also ask

How do you remove HTML tags from data in PHP?

The strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped. This cannot be changed with the allow parameter.

How do I remove all tags from a string?

To strip out all the HTML tags from a string there are lots of procedures in JavaScript. In order to strip out tags we can use replace() function and can also use . textContent property, . innerText property from HTML DOM.

Which function is used to remove all HTML tags from string passed to a form?

Which function is used to remove all HTML tags from a string passed to a form? Explanation: The function strip_tags() is used to strip a string from HTML, XML, and PHP tags.

How do you remove tags in HTML?

Approach: Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.


2 Answers

use strip_tags

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>'; echo strip_tags($text);   //output Test paragraph. Other text  <?php echo substr(strip_tags($row_get_Business['business_description']),0,110) . "..."; ?> 
like image 111
Yogesh Suthar Avatar answered Oct 12 '22 01:10

Yogesh Suthar


Use PHP's strip_tags() function.

For example:

$businessDesc = strip_tags($row_get_Business['business_description']); $businessDesc = substr($businessDesc, 0, 110);   print($businessDesc); 
like image 44
EM-Creations Avatar answered Oct 12 '22 03:10

EM-Creations