Search code examples
phphtmlsubstr

As only display text with substr()


On the basis of data I have saved an article that comes with HTML as <br>, <table> ... etc. ....

example :

<tr>
    <td style="width:50%;text-align:left;">Temas nuevos - Comunidad</td>
    <td class="blocksubhead" style="width:50%;text-align:left;">Temas actualizados - Comunidad Temas actualizados - Comunidad</td>
</tr>

What I want is displayed on another screen a summary of the article using substr (), my problem is I can not print what I want, and that prints the html code eta first.

Example: echo substr($row["news"], 0, 20);

It is printing the first 20 characters, it only show at browser:

<td style="width:50%;text-align:l<td/>

What I want is, it only show the text and discard the html code it has


Solution

  • Use strip_tags() to strip html etc from the string...

    So: echo substr(strip_tags($row["news"]), 0, 20);

    http://php.net/manual/en/function.strip-tags.php

    You could also do it using preg_replace() to match and replace anything that looks like a tag :)