Setting Substr to Longer String Length

This is a more important issue for executing PHP.

What are the positives or negatives for the following two methods for limiting row size?

function max_length($string, $length) {
  return (strlen($string) > $length)?substr($string, 0, $length):$string;
}

substr($string, 0, $length);

The case I'm worried about is when the string is less than the required length. Could this cause buffer overflow errors? Extra spaces? Repeated characters?

The only thing I know is speed, substrat least 2x faster than the provided custom function.

+3
source share
2 answers

As you can see at http://us2.php.net/substr

    ,    
     ,    (  
 ).

, , , .

+3

, . - - . :

$maxString = substr($string, 0, $length);
+2

Source: https://habr.com/ru/post/1705140/


All Articles