Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: obciêcie tekstu a tagi html
Forum PHP.pl > Forum > Przedszkole
bronx
witam

mam taki problem

do obciêcia tekstu u¿ywa³em funkcji substr(), czyli tak


  1. <?php
  2. echo &#092;"tekst\".substr( $data[5], 0, 160 ).\"...\";
  3. ?>


ale teraz pojawi³ siê problem bo gdy w tekscie bêdzie jaki¶ tag htmlu (np. <a href=co¶tam.php target=blank cass=s>) to utnie mi go te¿ :/ wie kto¶ mo¿e jak wyj¶æ z takiej sytuacji questionmark.gif

pozdrawiam
bregovic
Cytat(http://php.net/substr)
Here's a little bit of code to chop strings (with html tags) around a specified length while making sure no html tags are chopped. It also prevents chopping while a tag is still open.

Note: this will only work in xhtml strict/transitional due to the checking of "/>" tags and the requirement of quotations in every value of a tag. It's also only been tested with the presence of br, img, and a tags, but it should work with the presence of any tag.
  1. <?php
  2. function html_substr($posttext, $minimum_length, $length_offset) {
  3.  // The approximate length you want the concatenated text to be
  4.  $minimum_length = 200;
  5.  // The variation in how long the text can be
  6.  // in this example text length will be between 200-10=190 characters
  7.  // and the character where the last tag ends
  8.  $length_offset = 10;
  9.  // Reset tag counter & quote checker
  10.  $tag_counter = 0;
  11.  $quotes_on = FALSE;
  12.  // Check if the text is too long
  13.  if (strlen($posttext) > $minimum_length) {
  14.  // Reset the tag_counter and pass through (part of) the entire text
  15.  for ($i = 0; $i < strlen($posttext); $i++) {
  16.  // Load the current character and the next one
  17.  // if the string has not arrived at the last character
  18.  $current_char = substr($posttext,$i,1);
  19.  if ($i < strlen($posttext) - 1) {
  20.  $next_char = substr($posttext,$i + 1,1);
  21.  }
  22.  else {
  23.  $next_char = &#092;"\";
  24.  }
  25.  // First check if quotes are on
  26.  if (!$quotes_on) {
  27.  // Check if it's a tag
  28.  // On a \"<\" add 3 if it's an opening tag (like <a href...)
  29.  // or add only 1 if it's an ending tag (like </a>)
  30.  if ($current_char == &#092;"<\") {
  31.  if ($next_char == &#092;"/\") {
  32.  $tag_counter++;
  33.  }
  34.  else {
  35.  $tag_counter = $tag_counter + 3;
  36.  }
  37.  }
  38.  // Slash signifies an ending (like </a> or ... />)
  39.  // substract 2
  40.  if ($current_char == &#092;"/\") $tag_counter = $tag_counter - 2;
  41.  // On a \">\" substract 1
  42.  if ($current_char == &#092;">\") $tag_counter--;
  43.  // If quotes are encountered, start ignoring the tags
  44.  // (for directory slashes)
  45.  if ($current_char == &#092;"\"\") $quotes_on = TRUE;
  46.  }
  47.  else {
  48.  // IF quotes are encountered again, turn it back off
  49.  if ($current_char == &#092;"\"\") $quotes_on = FALSE;
  50.  }
  51.  
  52.  // Check if the counter has reached the minimum length yet,
  53.  // then wait for the tag_counter to become 0, and chop the string there
  54.  if ($i > $minimum_length - $length_offset && $tag_counter == 0) {
  55.  $posttext = substr($posttext,0,$i + 1) . &#092;"...\";
  56.  return $posttext;
  57.  }
  58.  }
  59.  }
  60.  return $posttext;
  61. }
  62. ?>
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.