Salve ragazzi,
ho cercato nel forum qualcosa che riuscisse a risolvere il mio problema ma in parte ci sono riuscito ed in parte no :/
In pratica, ho una funzione che mi "taglia" una stringa dopo N caratteri che gli passo.
E qui tutto ok.. logicamente all'interno della stringa possono trovarci dei tag tra cui (<b> </b> <i> </i> etc..)
Quindi se dentro la stringa prelevata rimane qualche tag aperto, devo provvedere a chiuderli.
Quindi dentro la funzione mi splitto tutti blocchi di parole e analizzo quanti TAG trovo (aperti e chiusi) con i relativi contatori.
Alla fine se ci sono dei contatori diversi da 0 li ciclo chiudendo i rispettivi TAG fino all'azzeramento del contatore.
Ora.. sono riuscito a riconoscere i seguenti tag : <b> </b> <i> </i> <u> </u>
Ma non riesco a riconoscere i div e gli strong soprattutto se tra <> vi è un spazio (es: <div style="..) oppure (es: <strong style"..">"
Qui di seguito il codice che sto cercando di far funzionare: Spero che qualcuno mi possa aiutare
codice:function TagliaStringa($stringa, $max_char){ if(strlen($stringa)>$max_char){ $stringa_tagliata=substr($stringa, 0,$max_char); $last_space=strrpos($stringa_tagliata," "); $stringa_ok=substr($stringa_tagliata, 0,$last_space); $TagI = 0; $TagB = 0; $TagU = 0; $TagA = 0; $TagS = 0; $TagD = 0; $i=0; $Stringa2 = ""; $arrstr = explode(" ",$stringa_ok); while($i < count($arrstr)) { $Stringa2.= str_replace("<br>"," ",$arrstr[$i]); $Stringa2.= " "; if (preg_match_all('/<([A-Za-z])[^>]*?>/i', $arrstr[$i], $matches)) { foreach($matches[0] as $match) { if (strtolower($match)=="<i>") { $TagI = $TagI + 1; } elseif (strtolower($match)=="<b>") { $TagB = $TagB + 1; } elseif (strtolower($match)=="<u>") { $TagU = $TagU + 1; } elseif (strtolower($match)=="<a>") { $TagA = $TagA + 1; } elseif (strtolower($match)=="<div>") { $TagD = $TagD + 1; } elseif (strtolower($match)=="<div") { $TagD = $TagD + 1; } elseif (strtolower($match)=="<strong>") { $TagS = $TagS + 1; } elseif (strtolower($match)=="<strong") { $TagS = $TagS + 1; } } } if (preg_match_all('/<\/([A-Za-z])[^>]*?>/i', $arrstr[$i], $matches)) { foreach($matches[0] as $match) { if (strtolower($match)=="</i>") { $TagI = $TagI - 1; } elseif (strtolower($match)=="</b>") { $TagB = $TagB - 1; } elseif (strtolower($match)=="</u>") { $TagU = $TagU - 1; } elseif (strtolower($match)=="</a>") { $TagA = $TagA - 1; } elseif (strtolower($match)=="</div>") { $TagD = $TagD - 1; } elseif (strtolower($match)=="</strong>") { $TagS = $TagS - 1; } } } $i++; } if ($TagI!=0) { do { $Stringa2.= "</i>"; $TagI++; } while ($TagI = 0); } if ($TagB!=0) { do { $Stringa2.= "</b>"; $TagB++; } while ($TagB = 0); } if ($TagU!=0) { do { $Stringa2.= "</u>"; $TagU++; } while ($TagU = 0); } if ($TagA!=0) { do { $Stringa2.= "</a>"; $TagA++; } while ($TagA = 0); } if ($TagS!=0) { do { $Stringa2.= "</strong>"; $TagS++; } while ($TagS = 0); } if ($TagD!=0) { do { $Stringa2.= "</div>"; $TagD++; } while ($TagD = 0); } return $Stringa2."..."; }else{ return $stringa; } }

Rispondi quotando

