Avatar billede domaz.dk Novice
30. januar 2006 - 19:00 Der er 1 løsning

forkort tekst uden at "klippe" i html-tags

Jeg har fundet det her script der virker ganske fint der kan forkorte ens tekst og samtidig tage højde for at den ikke "klipper" mit i et HTML-tag.. men det virker kun i XHTML... :/

Nogle der har en god idé til hvordan det kunne bygges op så den også virker i HTML?


scriptet:

<?php
function a_shortentext($posttext, $minimum_length = 200, $length_offset = 20, $cut_words = FALSE, $dots = TRUE) {
  // $minimum_length:
  // The approximate length you want the concatenated text to be 


  // $length_offset:
  // The variation in how long the text can be in this example text
  // length will be between 200 and 200-20=180 characters and the
  // character where the last tag ends

  // Reset tag counter & quote checker
  $tag_counter = 0;
  $quotes_on = FALSE;
  // Check if the text is too long
  if (strlen($posttext) > $minimum_length) {
      // Reset the tag_counter and pass through (part of) the entire text
      $c = 0;
      for ($i = 0; $i < strlen($posttext); $i++) {
          // Load the current character and the next one
          // if the string has not arrived at the last character
          $current_char = substr($posttext,$i,1);
          if ($i < strlen($posttext) - 1) {
              $next_char = substr($posttext,$i + 1,1);
          }
          else {
              $next_char = "";
          }
          // First check if quotes are on
          if (!$quotes_on) {
              // Check if it's a tag
              // On a "<" add 3 if it's an opening tag (like <a href...)
              // or add only 1 if it's an ending tag (like </a>)
              if ($current_char == '<') {
                  if ($next_char == '/') {
                      $tag_counter += 1;
                  }
                  else {
                      $tag_counter += 3;
                  }
              }
              // Slash signifies an ending (like </a> or ... />)
              // substract 2
              if ($current_char == '/' && $tag_counter <> 0) $tag_counter -= 2;
              // On a ">" substract 1
              if ($current_char == '>') $tag_counter -= 1;
              // If quotes are encountered, start ignoring the tags
              // (for directory slashes)
              if ($current_char == '"') $quotes_on = TRUE;
          }
          else {
              // IF quotes are encountered again, turn it back off
              if ($current_char == '"') $quotes_on = FALSE;
          }
         
          // Count only the chars outside html tags
          if($tag_counter == 2 || $tag_counter == 0){
              $c++;
          }         
                         
          // Check if the counter has reached the minimum length yet,
          // then wait for the tag_counter to become 0, and chop the string there
          if ($c > $minimum_length - $length_offset && $tag_counter == 0 && ($next_char == ' ' || $cut_words == TRUE)) {
              $posttext = substr($posttext,0,$i + 1);             
              if($dots){
                  $posttext .= '...';
              }
              return $posttext;
          }
      }
  } 
  return $posttext;

}
?>
Avatar billede domaz.dk Novice
06. februar 2006 - 05:43 #1
lukker
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester

Seneste spørgsmål Seneste aktivitet
I går 21:00 Libre Office Impress Af Frank i Andre styresystemer
I går 11:47 VB script Af Jenshentze i Word
I går 11:21 Popup ved opstart Af mort1 i Windows
04/0918:50 Slet lokal konto Af ErikHg i Windows
04/0916:05 Ændre tal i en celle Af xvid i Excel