-1

i get the following notice in php : Strict standards: Only variables should be passed by reference in get_id.php on line 27

the code is :

<?php
function truncate($text, $length, $suffix ='', $isHTML = true) {
    $i = 0;
    $simpleTags=array('br'=>true,'hr'=>true,'input'=>true,'image'=>true,'link'=>true,'meta'=>true);
    $tags = array();
    if($isHTML){
        preg_match_all('/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
        foreach($m as $o){
            if($o[0][1] - $i >= $length)
                break;
            $t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);
            // test if the tag is unpaired, then we mustn't save them
            if($t[0] != '/' && (!isset($simpleTags[$t])))
                $tags[] = $t;
            elseif(end($tags) == substr($t, 1))
                array_pop($tags);
            $i += $o[1][1] - $o[0][1];
        }
    }

    // output without closing tags
    $output = substr($text, 0, $length = min(strlen($text),  $length + $i));
    // closing tags
    $output2 = (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '');

    // Find last space or HTML tag (solving problem with last space in HTML tag eg. <span class="new">)
    $pos = (int)end(end(preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE)));
    // Append closing tags to output
    $output.=$output2;

    // Get everything until last space
    $one = substr($output, 0, $pos);
    // Get the rest
    $two = substr($output, $pos, (strlen($output) - $pos));
    // Extract all tags from the last bit
    preg_match_all('/<(.*?)>/s', $two, $tags);
    // Add suffix if needed
    if (strlen($text) > $length) { $one .= $suffix; }
    // Re-attach tags
    $output = $one . implode($tags[0]);

    //added to remove  unnecessary closure
    $output = str_replace('</!-->','',$output); 

    return $output;
}
?>

how do i remove this notice.. Please Help. I am using WampServer 2.4. This notice won't show in older version and in linux hosting. but the new wampserver shows this notice. Thanks in advance...

Tejas Rana
  • 49
  • 10
  • 6
    General tip: if you post a wall of code and just throw a line number at is, INDICATE WHERE THAT LINE IS. Don't force us to count lines manually. As well, don't use regexes for parsing html. You end up with hideous monstrosities like this. Use DOM instead. – Marc B Jul 24 '13 at 14:54
  • "Only variables should be passed by reference in get_id.php on line 27" - I guess it doesn't have to do with wampserver. I guess it would be the php-version that matters. – bestprogrammerintheworld Jul 24 '13 at 15:21
  • possible duplicate of [Strict Standards: Only variables should be passed by reference](http://stackoverflow.com/questions/2354609/strict-standards-only-variables-should-be-passed-by-reference) – Lorenz Meyer Jun 21 '14 at 08:23

2 Answers2

0

there are a lot of similiar questions here.

end expects its parameter to be a passed by reference, and only variables can be passed by reference (not the return value of another function, like preg_split in your case)

easiest solution should be to split the calls to separate lines, e.g.

$splitted = preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE);
$last_item = end($splitted);
$very_last_item = end($last_item);

or, if you have the time and stuff, rework this function so you don't have to end() calls (sorry, I kinda can't get into what you want to accomplish here)

mgrueter
  • 1,400
  • 6
  • 15
  • Thanks it's work thank a lot.. this was my first question. you all are very fast to solve this thanks thanks a lot... – Tejas Rana Jul 25 '13 at 13:43
0

It’s end() that expects a reference because it changes the internal array value pointer