Shorten the image CAPTION

V1.0 Shorten the CAPTION text

When using the CPs image <div> and image special, the caption text is to be shortened for the the simple FE output. In LightBox the whole caption text is available.

Assumed template entry:

[CAPTION]<p style="margin:3px 0 0 0; width:200px; height:15px;"><span class="cut_caption">{CAPTION}</span></p>[/CAPTION]
[CAPTION_ELSE]<p>{IMGNAME}</p>[/CAPTION_ELSE]

It is triggered to the character sequence <span class=“cut_caption”> cption-text </span>.
caption-Text is thus separated, selected and processed.
The place <span …> …. </span> is signed over with the result.

In this example the character stream substr('$1', 0, 8) from position 0 to 8 is cutting out and returned.

Result: Yes We Can do many things with this cmsYes We C


cp_trig_cut_caption_text.php

<?php
// -------------------------------------------------------------------------------------------
// obligate check for phpwcms constants
  if (!defined('PHPWCMS_ROOT')) {
    die("You Cannot Access This Script Directly, Have a Nice Day."); }
// -------------------------------------------------------------------------------------------
 
// Cutting the caption text in FE output  (substr('$1', 0, 8) = cutting out charakter 0 to 8)
// K.Heermann (flip-flop) 17.05.09
 
/*
* cp_trigger_function_name - the unique function name
* $text - holds the content part html source on which you can parse or do custom processing
* $data - is a reference to an array which holds content part values like ID, dates and other values - see db table phpwcms_articlecontent
 
Always return $text;
*/
 
 
/* e.g. Template -------------------------------------------------------------
[CAPTION]<p style="margin:3px 0 0 0; width:200px; height:15px;"><span class="cut_caption">{CAPTION}</span></p>[/CAPTION]
[CAPTION_ELSE]<p>{IMGNAME}</p>[/CAPTION_ELSE]
----------------------------------------------------------------------------- */
 
   function CP_CUT_CAPTION_TEXT($text, & $data) {
 
 
   // CPs: 29 => image <div>, 31 => image special
      if(($data['acontent_type'] == 29 OR $data['acontent_type'] == 31))
      {
        $text = preg_replace("/<span class=\"cut_caption\">(.*?)<\/span>/e","substr('$1', 0, 8).' ...'" , $text);
      }
 
      return $text;
   }
 
   register_cp_trigger('CP_CUT_CAPTION_TEXT');
 
?>


V1.1.1 Shorten the image CAPTION text

When using the CPs image <div> and image special, the caption text is to be shortened for the the simple FE output. In LightBox the whole caption text is available.
This is reached by the employment of a CP-trigger in the folder /template/inc_script/frontend_init/

Into the Template (see below) an exactly defined character sequence is built around the {CAPTION}-placeholder. This character sequence is evaluated and refined by the trigger program in …/frontend_render/. Parameters can be handed over by a simple class name from the Template to the program.

Assumed template entry:

[CAPTION]<p style="margin:3px 0 0 0; width:200px; height:15px;"><span class="cut_caption-c20-w3">{CAPTION}</span></p>[/CAPTION]
[CAPTION_ELSE]<p>{IMGNAME}</p>[/CAPTION_ELSE]

The character sequence <span class=“cut_caption-c20-w3”> Caption-Text </span> is triggered by the /frontend_init/-program.
Caption-Text is thus separated, selected and processed.

V1.1.1: Additionally the parameters in cut_caption-c20-w3 are recognized and processed.

Parameter notation:

  • -cXX → Display of max. XX characters autom. switch into the mode [char]
  • -wXX → Display of max. XX characters autom. switch into the mode [word]

If both parameters are indicated, the following is valid: If the string “XX words” in mode [ word ] is longer than max. characters indicated in -cXX, there is switched autom. into the mode [char].

The place <span …> …. </span> is signed over with the result.

In this example the first three words spent, if available, or if the word string should be longer than 20 characters, these 20 character string is spent.


Given character string: Yes We Can do many things with this cms

Template call Result Comment
<span class=“cut_caption-c20-w3”> Yes We Can … 20 characters / 3 words
<span class=“cut_caption-c8-w6”> Yes We C … 8 characters / 6 words
<span class=“cut_caption-w6”> Yes We Can do many things … 6 words


cp_trig_cut_caption_text.php

<?php
// -------------------------------------------------------------------------------------------
// obligate check for phpwcms constants
  if (!defined('PHPWCMS_ROOT')) {
    die("You Cannot Access This Script Directly, Have a Nice Day."); }
// -------------------------------------------------------------------------------------------
 
// Cutting the caption text in FE output V1.1.1
// K.Heermann (flip-flop) 18.05.09
 
/*
* cp_trigger_function_name - the unique function name
* $text - holds the content part html source on which you can parse or do custom processing
* $data - is a reference to an array which holds content part values like ID, dates and other values - see db table phpwcms_articlecontent
 
Always return $text;
*/
 
 
/* e.g. Template -------------------------------------------------------------
[CAPTION]<p style="margin:3px 0 0 0; width:200px; height:15px;"><span class="cut_caption-c20-w3">{CAPTION}</span></p>[/CAPTION]
[CAPTION_ELSE]<p>{IMGNAME}</p>[/CAPTION_ELSE]
----------------------------------------------------------------------------- */
 
 
/* -----------------------------------------------------------------------------
 [$text=whole string]
 
 [$param=mode  e.g. -c20-w3]
 -cX  =  max characters to show
 -wX  =  max words to show  (if word string > max charakters, max.char is used)
 
 Yes, I know the php function str_word_count,
 but there is a problem with utf-8 Sonderzeichen like ä, ü, ö ....
 ----------------------------------------------------------------------------- */
 
function String_Word_Limiter($text, $param=''){
 
  // Default, if there is no parameter from client
  $max_char       = 100;
  $limit_word     =   4;
  $prio_char_word = 'char';
 
  if (!empty($text) ) {              // parameter set by user ?
    $string = $text;                 // Yes and save
    $param = strtolower($param);     // convert all to lower char
    $_param = explode('-',$param);   // Cut out all parameters $_param[1] - [2]
 
    foreach ($_param as $key=>$value) {  // Cutting out and allocate the parameters
      if (substr($value, 0, 1) == 'c') {
        $max_char = substr($value, 1);
        $prio_char_word = 'char';
      }
      elseif (substr($value, 0, 1) == 'w') {
        $limit_word = substr($value, 1);
        $prio_char_word = 'word';
      }
    }
  } else {                               // No parameter set by user
     $string = $param;
    }
 
   if ($prio_char_word == 'word') {      // Is it the word mode?
     $explode = explode(' ',$text);      // Cut out all words
     $string  = '';
 
 
     for($i=0; $i<$limit_word; $i++){    // Generate the new word string
        $string .= $explode[$i]." ";
     }
     // Fallback, if word string is too lang
     if (strlen($string) > $max_char ) $string = substr($text, 0, $max_char);
 
   }
   // It is the Charakter mode
   else $string = substr($text, 0, $max_char);
 
 
   $dots = (strlen($text) > strlen($string) ) ? ' ...' : ''; // Dots set
 
    return $string.$dots;
}
 
// ----------------------------------------------------------------------------
 
   function CP_CUT_CAPTION_TEXT($text, & $data) {
 
 
   // CPs: 29 => image <div>, 31 => image special
      if(($data['acontent_type'] == 29 OR $data['acontent_type'] == 31))
      {
        //<span class="cut_caption">Caption-Text</span>  // $1 = -c20-w4
        // e.g. <span class="cut_caption-c20-w4">Caption-Text</span>
 
        $search = "/<span class=\"cut_caption(.*?)\">(.*?)<\/span>/e";
        $replace = "String_Word_Limiter('$2','$1')";
 
        $text = preg_replace($search,$replace , $text);
 
      }
 
      return $text;
   }
 
   register_cp_trigger('CP_CUT_CAPTION_TEXT');
 
?>
english/phpwcms_replacer_rts/frontend_init/cp_trigger/shorten-img-caption-text.txt · Last modified: 2018/06/03 18:09 (external edit)
www.planmatrix.de www.chimeric.de Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0