Forums

Error in Module Character set

andrius 04 Feb, 2009
Hello, Max,
I an testing your comments.

In the Latest comment module some Lithuanian national characters at the end of the line do not show correctly.
Please, see attachment.

How can I fix that?
Max_admin 04 Feb, 2009
Hi andrius,

do they show correctly in the admin area ?

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 05 Feb, 2009
Hi andrius,

Ahhh . . . the problem is that teh code is using substr() with UTF characters and it doesn't correctly find the character breaks.

If you have multi-byte string mbstring PHP extension installed then mb_substr would presumably work.

If not, then in the PHP Manual Notes for substr() there are a couple of alternatives suggested - functions that only break on spaces; and functions that try to recognise UTF characters correctly and only break on character boundaries.

Adding one of these would probably fix the problem.
// function to break on spaces
function str_stop($string, $max_length)
{
  if ( strlen($string) > $max_length  ){
    $string = substr($string, 0, $max_length);
    $pos = strrpos($string, " ");
    if($pos === false) {
      return substr($string, 0, $max_length)."...";
    }
    return substr($string, 0, $pos)."...";
  } else {
    return $string;
  }
}
<?php
function utf8_substr($string, $start) 
{
  preg_match_all("/./su", $string, $ar);
  if ( func_num_args() >= 3 ) {
    $end = func_get_arg(2);
    return join("", array_slice($ar[0], $start, $end));
  } else {
    return join("", array_slice($ar[0], $start));
  }
}
?>
Neither of these are tested here - I think I've used the first function successfully before.

Bob
andrius 05 Feb, 2009
Hello, Bob,
Thank you for an answer.
I am not so advanced user... Would you tell me exactly in which file and what place do I have to insert this code?
GreyHead 05 Feb, 2009
Hi andrius,

OK = let's try the Admin first and see if we can get a fix to work.

Please make a backup copy of admin.chronocomments.html.php and put it somewhere safe.

Open the original copy and go to line 211 - should be an empty line just before the last ?>

Paste this function in there
    // function to break on spaces
    function str_stop($string, $dummy, $max_length)
    {
      if ( strlen($string) > $max_length  ){
        $string = substr($string, 0, $max_length);
        $pos = strrpos($string, " ");
        if($pos === false) {
          return substr($string, 0, $max_length)."...";
        }
        return substr($string, 0, $pos)."...";
      } else {
        return $string;
      }
    }
there should then be two } before the function and still the ?> after it.

Go to line 87 which should read
<td width="40%" align="left" ><?php echo substr($row->text, 0, 50).' .....'; ?></td>
and replace it with
<td width="40%" align="left" ><?php echo str_stop($row->text, 0, 50).' .....'; ?></td>
Save the file and see if this works - if it doesn't we can try the other function.

Bob
andrius 05 Feb, 2009
Hi,
I replaced the code, tried to enter comments administration and got this error message: "Parse error: syntax error, unexpected $end in /home/ecoauto/domains/mem.lt/public_html/administrator/components/com_chronocomments/admin.chronocomments.html.php on line 224"

Sorry, didn't help...
GreyHead 05 Feb, 2009
Hi andrius,

Two possibilities, let's try once more. From line 206 on paste this code - the first couple of lines should overlap what is there now.
        </form>
	<?php
	}
    // function to break on spaces
    function str_stop($string, $dummy, $max_length)
    {
        if ( strlen($string) > $max_length  ){
            $string = substr($string, 0, $max_length);
            $pos = strrpos($string, " ");
            if($pos === false) {
                return substr($string, 0, $max_length)."...";
            }
            return substr($string, 0, $pos)."...";
        } else {
            return $string;
        }
    }
}
?>

Bob
andrius 05 Feb, 2009
Hi Bob,
Changed the code, but still no luck...
I can see the same question marks. But no Fatal errors 🙂

Any other ideas?
Max_admin 06 Feb, 2009
Hi andrius,

please open admin.Chronocomments.html.php and find around line 87 this line:

<td width="40%" align="left" ><?php echo substr($row->text, 0, 50).' .....'; ?></td>


please replace it with :

<td width="40%" align="left" ><?php echo JString::substr($row->text, 0, 50).' .....'; ?></td>


now please test the admin area and let me know if the problem is solved ?

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
andrius 07 Feb, 2009
Hi, Max,
Now the characters are shown correct in admin panel.
(note, that I changed only the 87 line and no other changes were made)

What should I change to correct the problem at the Front-end?
GreyHead 07 Feb, 2009
Hi andrius,

Good catch by Max to find the Joomla solution - much better than the PHP functions.

I think that the front-end needs a change in line 57 of root/modules/mod_chronocomments/helper.php where
substr(modChronocommentsHelper::censor(modChronocommentsHelper::bb2html(modChronocommentsHelper::cleanBB($commentx->text))), 0, $params->get( 'limittext', 0 ))
needs to become
JString::substr(modChronocommentsHelper::censor(modChronocommentsHelper::bb2html(modChronocommentsHelper::cleanBB($commentx->text))), 0, $params->get( 'limittext', 0 ))

Bob
andrius 07 Feb, 2009
Hello,
Yes, problem solved 😀
Thank you for your help...

Now the special characters in the back-end and in the latest comment module front-end are shown correct 😀

Andrius S.
Max_admin 08 Feb, 2009
Great news, thanks for letting me know!🙂

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.