Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z niedomknietymi tagami...
Forum PHP.pl > Forum > PHP
marekk
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 winksmiley.jpg
Apo
hmm ciekawy problem poruszyłeś smile.gif Gotowej funkcji raczej do tego nie znajdziesz ale mozesz pisać różne wyrażenia regularne.

  1. <?php
  2. $string = preg_replace_callback('/</(b|i|u)(.)/si', 'function_', $string);
  3.  
  4. function function_($array)
  5. {
  6. $str = '</'.$array[0];
  7. $str .= '>';
  8.  
  9. return $str;
  10. }
  11. ?>


Troche prymitywny sposób tu pokazałem, pisałem z palca. Zaniedługo będe pisał większą klasę do obsługi bbcode z sprawdzaniem kolejności zamkniętych tagów itp, jak skończe to pokaże kod smile.gif

Pozdrawiam
marekk
No własnie tak podejrzewałem, że gotowej funkcji raczej nie ma... no chyba, że napisana przez kogoś smile.gif

Dzieki choć nie wiem czy sobie poradze z tym bo jak pisałem coś kiepsko mi idzie z tymi wyrażeniami regularnymi. No ale może jakoś inaczej teraz obejdę ten problem a zgłosze się za jakiś czas sprawdzić czy może udało się stowrzyć tą klasę smile.gif
MatheW
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 tongue.gif
marekk
Dzięki! smile.gif Funkcja działa ok. chociaż najpierw myslałem, ze cos nie tak z nia... skopiowałem oryginał z manuala i też to samo... hehh okazało sie, że ona nic nie wyświetla po prostu ale zamyka tagi i już dalej inny tekst jest poprawnie wyświetlany smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.