Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Problem z niedomknietymi tagami...
marekk
post
Post #1





Grupa: Zarejestrowani
Postów: 65
Pomógł: 0
Dołączył: 27.05.2003

Ostrzeżenie: (0%)
-----


Mam taki problem, założmy ze ktoś dopisuje teksty używjaąc tagów <i>, <b> itd. Jak wiadomo zdarzaja sie czasami niedomkniecia tymbardziej jeśli wpisujemy tagi ręcznie i wówczas na stronie mamy dalsze teksty niepoprawnie sformatowane. Szukałem troche na forum ale konkretnie tego problemu nie udało mi się znależć.

Podejrzewam, że da się to zrobić jakimś jednym zapytaniem z wyrażeniami regularnymi ale pryzznam jestem w tym kiepski :] A problem chyba nie jest taki znów banalny, moze jest też jakaś gotowa funkcja php-a? Z mojej strony obojetne cyz funkcja będzie działac w momencie dodawania wpisów do bazy czy pozniejszego wyświetlania. Prosiłbym o jakieś przykłady lub nakierunkowanie na rozwiązanie (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Ten post edytował marekk 8.08.2006, 19:06:55
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
MatheW
post
Post #2





Grupa: Zarejestrowani
Postów: 135
Pomógł: 0
Dołączył: 29.05.2006
Skąd: Lublin

Ostrzeżenie: (0%)
-----


Kod
function autoCloseTags($string) {
// automatically close HTML-Tags
// (usefull e.g. if you want to extract part of a blog entry or news as preview/teaser)
// coded by Constantin Gross <connum at googlemail dot com> / 3rd of June, 2006
// feel free to leave comments or to improve this function!

$donotclose=array('br','img','input', 'hr'); //Tags that are not to be closed

//prepare vars and arrays
$tagstoclose='';
$tags=array();

//put all opened tags into an array
preg_match_all("/<(([A-Z]|[a-z]).*)(( )|(>))/isU",$string,$result);
$openedtags=$result[1];
$openedtags=array_reverse($openedtags); //this is just done so that the order of the closed tags in the end will be better

//put all closed tags into an array
preg_match_all("/<\/(([A-Z]|[a-z]).*)(( )|(>))/isU",$string,$result2);
$closedtags=$result2[1];

//look up which tags still have to be closed and put them in an array
for ($i=0;$i<count($openedtags);$i++) {
   if (in_array($openedtags[$i],$closedtags)) { unset($closedtags[array_search($openedtags[$i],$closedtags)]); }
       else array_push($tags, $openedtags[$i]);
}

//prepare the close-tags for output
for($x=0;$x<count($tags);$x++) {
$add=strtolower(trim($tags[$x]));
if(!in_array($add,$donotclose)) $tagstoclose.='</'.$add.'>';
}

//and finally
return $tagstoclose;
}
manual sie klania (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 6.10.2025 - 16:40