with texto tag remove limpiar from etiquetas eliminar allow all php substr

texto - ¿Substr PHP pero mantener las etiquetas HTML?



remove html tags from string (3)

Al usar @newbieuser su función, tuve el mismo problema, como @ pablo-pazos, que no se estaba rompiendo cuando $ limit cayó en una etiqueta html (en mi caso <br /> en la r)

Arreglado con algun codigo

if ( strlen($code) <= $limit ){ return $code; } $html = substr($code, 0, $limit); //We must find a . or > or space so we are sure not being in a html-tag! //In my case there are only <br> //If you have more tags, or html formatted text, you must do a little more and also use something like http://htmlpurifier.org/demo.php $_find_last_char = strrpos($html, ".")+1; if($_find_last_char > $limit/3*2){ $html_break = $_find_last_char; }else{ $_find_last_char = strrpos($html, ">")+1; if($_find_last_char > $limit/3*2){ $html_break = $_find_last_char; }else{ $html_break = strrpos($html, " "); } } $html = substr($html, 0, $html_break); preg_match_all ( "#<([a-zA-Z]+)#", $html, $result ); ......

Me pregunto si hay una forma elegante de recortar un texto, pero, ¿teniendo en cuenta la etiqueta HTML?

Por ejemplo, tengo esta cadena:

$data = ''<strong>some title text here that could get very long</strong>'';

Y digamos que necesito devolver / generar esta cadena en una página, pero me gustaría que no tenga más de X caracteres. Digamos 35 para este ejemplo.

Luego uso:

$output = substr($data,0,20);

Pero ahora termino con:

<strong>some title text here that

que, como puede ver, las etiquetas fuertes de cierre se descartan, lo que rompe la visualización HTML.

¿Hay alguna forma de evitar esto? También tenga en cuenta que es posible tener varias etiquetas en la cadena, por ejemplo:

<p>some text here <strong>and here</strong></p>


Hace unos meses, creé una función especial que es la solución para su problema.

Aquí hay una función:

function substr_close_tags($code, $limit = 300) { if ( strlen($code) <= $limit ) { return $code; } $html = substr($code, 0, $limit); preg_match_all ( "#<([a-zA-Z]+)#", $html, $result ); foreach($result[1] AS $key => $value) { if ( strtolower($value) == ''br'' ) { unset($result[1][$key]); } } $openedtags = $result[1]; preg_match_all ( "#</([a-zA-Z]+)>#iU", $html, $result ); $closedtags = $result[1]; foreach($closedtags AS $key => $value) { if ( ($k = array_search($value, $openedtags)) === FALSE ) { continue; } else { unset($openedtags[$k]); } } if ( empty($openedtags) ) { if ( strpos($code, '' '', $limit) == $limit ) { return $html."..."; } else { return substr($code, 0, strpos($code, '' '', $limit))."..."; } } $position = 0; $close_tag = ''''; foreach($openedtags AS $key => $value) { $p = strpos($code, (''</''.$value.''>''), $limit); if ( $p === FALSE ) { $code .= (''</''.$value.''>''); } else if ( $p > $position ) { $close_tag = ''</''.$value.''>''; $position = $p; } } if ( $position == 0 ) { return $code; } return substr($code, 0, $position).$close_tag."..."; }

Aquí está DEMO: http://sandbox.onlinephpfunctions.com/code/899d8137c15596a8528c871543eb005984ec0201 (haga clic en "Ejecutar código" para verificar cómo funciona).


substr (strip_tags ($ contenido), 0, 100)