Ho diverse stringhe HTML da tagliare a 100 caratteri (del contenuto sottoposto a stripping, non dell'originale) senza rimuovere tag e senza infrangere HTML.Uso di substr.s di PHP() e strip_tags() pur mantenendo la formattazione e senza interrompere HTML
stringa HTML originale (288 caratteri):
$content = "<div>With a <span class='spanClass'>span over here</span> and a
<div class='divClass'>nested div over <div class='nestedDivClass'>there</div>
</div> and a lot of other nested <strong><em>texts</em> and tags in the air
<span>everywhere</span>, it's a HTML taggy kind of day.</strong></div>";
assetto standard: Trim a 100 caratteri e le interruzioni di HTML, contenuti spogliato tratta di ~ 40 caratteri:
$content = substr($content, 0, 100)."..."; /* output:
<div>With a <span class='spanClass'>span over here</span> and a
<div class='divClass'>nested div ove... */
Stripped HTML: Le uscite correggono il numero di caratteri ma ovviamente perdono la formattazione:
$content = substr(strip_tags($content)), 0, 100)."..."; /* output:
With a span over here and a nested div over there and a lot of other nested
texts and tags in the ai... */
soluzione parziale: utilizzando HTML Tidy o depuratore per chiudere le uscite tag HTML pulito, ma 100 caratteri di HTML non vengono visualizzati contenuti.
$content = substr($content, 0, 100)."...";
$tidy = new tidy; $tidy->parseString($content); $tidy->cleanRepair(); /* output:
<div>With a <span class='spanClass'>span over here</span> and a
<div class='divClass'>nested div ove</div></div>... */
Sfide: Per output HTML pulito e n caratteri (escluso il conteggio dei caratteri di elementi HTML):
$content = cutHTML($content, 100); /* output:
<div>With a <span class='spanClass'>span over here</span> and a
<div class='divClass'>nested div over <div class='nestedDivClass'>there</div>
</div> and a lot of other nested <strong><em>texts</em> and tags in the
ai</strong></div>...";
Domande simili
Direi incredibile, funziona, esattamente come la sfida delinea ... –
Ho ancora cose come 'inviarci un'email a Read More' tutto il tempo. Immagino che qualcosa non vada nel mio contenuto proveniente dal database ma qualcuno ha un'idea? Grazie! – TomShreds
Che ne dici del supporto di utf-8? –