Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> obcięcie tekstu a tagi html
bronx
post
Post #1





Grupa: Zarejestrowani
Postów: 333
Pomógł: 0
Dołączył: 4.03.2004

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


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
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 1)
bregovic
post
Post #2





Grupa: Zarejestrowani
Postów: 562
Pomógł: 15
Dołączył: 8.08.2003
Skąd: Denmark/Odense

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


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. ?>


--------------------
Prank - for the fun. Mac - for the simplicity. Deviantart - for the kick.
Life is ours, We live it our way -- Metallica
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 21.08.2025 - 03:37