2010-09-23 16 views
12

Ho il seguente codice:Perché gli strip_tags non funzionano in PHP?

<?php echo strip_tags($firstArticle->introtext); ?> 

Dove $ firstArticle è un oggetto stdClass:

object(stdClass)[422] 
    public 'link' => string '/maps101/index.php?option=com_content&view=article&id=57:greenlands-newest-iceberg&catid=11:geography-in-the-news' (length=125) 
    public 'text' => string 'GREENLAND'S NEWEST ICEBERG' (length=26) 
    public 'introtext' => string '<p>A giant chunk of ice calved off the Petermann Glacier on 

    the northwest side of Greenland this summer. At nearly 100 square miles (260 

    sq. km) in size, four times the size of Manhattan, th' (length=206) 
    public 'date' => 
    object(JDate)[423] 
     public '_date' => int 1284130800 
     public '_offset' => int 0 
     public '_errors' => 
     array 
      empty 

Si può vedere che $ firstArticle-> introtext si riferisce alla stringa:

"<p> Un'enorme fetta di ghiaccio si è staccata dal ghiacciaio Petermann sul lato nord-ovest della Groenlandia quest'estate: a quasi 100 miglia quadrate (260 km quadrati) di dimensioni, quattro volte più grandi di Manhattan,"

Il tag <p> è un problema per me in questa applicazione, tuttavia strip_tags si rifiuta assolutamente di rimuoverlo e non riesco a capire perché. Io in realtà rinunciato a strip_tags ed ho tentato di fare un preg_replace invece con la regex/< | *>/(\ n.):?

preg_replace('/<(.|\n)*?>/', '', $firstArticle->introtext); 

Ma che non ha funzionato neanche! Come posso rimuovere tutti i tag HTML (abbinati o meno) da questa stringa quando li output?

+4

Sei sicuro che non è '& lt: p >' che è lì dentro? – Wrikken

+0

Ho provato strip_tags() sulla tua stringa e funziona qui. – Evert

risposta

47

prova:

<?php echo strip_tags(html_entity_decode($firstArticle->introtext)); ?> 
+1

State cercando questo per secoli! Grazie. – hohner

+0

Ha funzionato per me, grazie! –

+0

Grazie per questo, ha risolto un problema che sto cercando di curare per ore. – Steve

6

molto curioso che striscia-tag non funziona ....

forse il vostro "<p>" è htmlentity-codificato? come "& lt; p & gt;" (Dare un'occhiata al codice sorgente della pagina)

otehrwise questo andrà a sostituire tutti i tag, quelli anche htmlentity codificati, ma è quasi ovvio che questo p-tag è semplicemente htmlentity-codificato in modo da provare che il primo ...

preg_replace('/(?:<|&lt;).*?(?:>|&gt;)/', '', $firstArticle->introtext); 
1

Nel mio caso, dovrei usare htmlspecialchars_decode($str);. html_entity_decode($firstArticle->introtext) non sembra funzionare per me.

A volte devo usare prima htmlentities.

 $txt = htmlentities($txt, null, 'utf-8'); 
     $txt = htmlspecialchars_decode($txt); 
Problemi correlati