Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: Ucinanie znakow - problem z nie zamknietymi tagami
Forum PHP.pl > Forum > Gotowe rozwi±zania
blitz
Siemanko
Mam takie problem..
zrobielm zajawke akutalnosci na stornie glownej.
Skrypt pobiera caly tekst aktulanosci i ucina do 300 znakow w taki sposob:
  1. <?php
  2.  
  3. //przycisk \"zobacz caly tekst\"
  4. $link= &#092;"(...) <a href=a/aktualnosci/$tid><img src=img/zobacz.gif border=0 align=absmiddle alt=\"Zobacz temat\"></a>\";
  5.  
  6. $tekst = substr($text, 0,300) . $link;
  7.  
  8. ?>


Problem w tym iz pobrany tekst zawiera tagi html (a,i,b,li, itd)
Skrypt ten niestety oblicza wszystkie znaki lacznie z tagami html i jak ucina w momencie np towrzenie linka, "psuje sie" i button "zobacz teamt" nie jest jest odnosiniekiem do wyswwieltnie calej aktualnosci, lub awet zdarza sie ze wyswietla przed buttonem znaki np:
Cytat
To jest tekst aktulanosci a tu pogrubiony te<b(i tu przycik "zobacz temat")


Moze istniej funkcja ktora ucina tylko zwykly tekst bez znakow lub moze domyka niedomniete znaki?
Czy ktos sie spotkal z takim czyms?

Pozdrawiam
Bakus
  1. <?php
  2. //przycisk \"zobacz caly tekst\"
  3. $link= &#092;"(...) <a href=a/aktualnosci/$tid><img src=img/zobacz.gif border=0 align=absmiddle alt=\"Zobacz temat\"></a>\";
  4.  
  5. $tekst = substr(strip_tags($text), 0,300) . $link;
  6. // ^ ^  ^
  7. ?>
blitz
Dzieki, z tym ze ta funkcja wywala calkowicie wszystkie tagi, wiec tekst leci jednym ciagiem baz jakiegolwiek formatowania a nie taki bylo zamierzenie cool.gif
Myslalem ze moze da sie to zrobic innym sposobem.

Pozdro
hwao
A ja sadze ze sposob jest juz i to od baaaaaaaaaardzo dlugo i mialbys go baaaaaaaaardzo szybko ale niestety baaaaaaaaaaardzo zle szukales
http://pl.php.net/substr
Komentarze?
  1. Here are the replacement functions for substr() and strlen() I use when support for html entities is required:
  2.  
  3. <?php
  4.  
  5. function html_strlen($str) {
  6. $chars = preg_split('/(&[^;s]+;)|/', $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  7. return count($chars);
  8. }
  9.  
  10. function html_substr($str, $start, $length = NULL) {
  11. if ($length === 0) return &#092;"\"; //stop wasting our time;)
  12.  
  13. //check if we can simply use the built-in functions
  14. if (strpos($str, '&') === false) { //No entities. Use built-in functions
  15.  if ($length === NULL)
  16.  return substr($str, $start);
  17.  else
  18.  return substr($str, $start, $length);
  19. }
  20.  
  21. // create our array of characters and html entities
  22. $chars = preg_split('/(&[^;s]+;)|/', $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE);
  23. $html_length = count($chars);
  24.  
  25. // check if we can predict the return value and save some processing time
  26. if (
  27.  ($html_length === 0) /* input string was empty */ or
  28.  ($start >= $html_length) /* $start is longer than the input string */ or
  29.  (isset($length) and ($length <= -$html_length)) /* all characters would be omitted */
  30.  )
  31.  return &#092;"\";
  32.  
  33. //calculate start position
  34. if ($start >= 0) {
  35.  $real_start = $chars[$start][1];
  36. } else { //start'th character from the end of string
  37.  $start = max($start,-$html_length);
  38.  $real_start = $chars[$html_length+$start][1];
  39. }
  40.  
  41. if (!isset($length)) // no $length argument passed, return all remaining characters
  42.  return substr($str, $real_start);
  43. else if ($length > 0) { // copy $length chars
  44.  if ($start+$length >= $html_length) { // return all remaining characters
  45.  return substr($str, $real_start);
  46.  } else { //return $length characters
  47.  return substr($str, $real_start, $chars[max($start,0)+$length][1] - $real_start);
  48.  }
  49. } else { //negative $length. Omit $length characters from end
  50.  return substr($str, $real_start, $chars[$html_length+$length][1] - $real_start);
  51. }
  52.  
  53. }
  54.  
  55. ?>
  56.  
  57. Example:
  58.  
  59. html_substr(\"&aacute;bla6bla\", 1, 4) -> \"bla6\"
  60.  
  61. If you happen to find any bugs, please let me know.

I cala baaaaaardzo madra reszta.
Baaaaardzo polecam,
pozdro
blitz
baaaaaaardzo dzieeeeeeekujeeee aarambo.gif ale juz to znaaaaaalazlem aarambo.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.