how can I put a condition in if so that it doesn’t happen if the array has "..." at the end of the sentence

Asked

Viewed 130 times

0

how can I put a condition on if(if (strlen($string) > $size) {) so it doesn’t happen if the array has "..." at the end of the sentence

$prod->setNome(Utility::limitaString($prod->getNome(), 30));

    public static function limitaString ($string, $size = 50) {
    if (strlen($string) > $size) {
        $string = substr($string, 0, $size);
        for ($i=$size; $i>=0; $i--) {
            $limiters = array(" ", "\n", "\t");
            if (in_array($string[$i], $limiters)) {
                if ($string[$i-1] == "\r")
                $i = $i-1;
                $string = substr($string, 0, $i);
                $string .= "...";
                return $string;
            }
        }
        $string .= "...";
    }
    return $string;
}

This function above is done to reduce the sentence to 30 characters and add "..." at the end of the sentence. but I need this condition for some already abbreviated sentence that I don’t want by the method.

how would be condition for array with at the end "...".

4 answers

2

Try this:

if (!stripos($string, "...") {
   //se ela achar as retiscencias ela não executa
   //seu codigo AQUI
}

What she’ll do is, look in her variable string if there is some part of the text "...", if not, it will execute your code. It searches throughout the $string ignoring the position, seeking only to find what was requested. I think this may solve your problem.

Link for the PHP function. Good luck!

2

I believe this should work:

$prod->setNome(Utility::limitaString($prod->getNome(), 30));

    public static function limitaString ($string, $size = 50) {
    if (strlen($string) > $size) {
        $string = substr($string, 0, $size);
        for ($i=$size; $i>=0; $i--) {
            $limiters = array(" ", "\n", "\t");
            if (in_array($string[$i], $limiters)) {
                if ($string[$i-1] == "\r")
                $i = $i-1;
                $string = substr($string, 0, $i);
                if( strstr($string,"...")){
                    //return null
                }else{
                    $string .= "...";
                }
                return $string;
            }
        }
        if( strstr($string,"...")){
             //return null
        }else{
             $string .= "...";
        }
    }
    return $string;
}

Note: I did not test.

2

I did not understand very well this array, but I will do what I understood:

preg_match(".{3}$", $string, $ultimos3);
if ($ultimos3[0] != "..." && strlen($string) > $size)

See working on Ideone.

What I did:

On the first line, I used a regular expression() to get the last 3 characters.

On Monday, it’s the same if which you were using, but now it checks whether the regex has returned ....

References:

  • Function preg_match in official php documentation.

2


You can use the function replace PHP, for this just inform a negative number.

Checks the size of the variable string and checks whether the last three characters of the variable string are not the three points.

if (strlen($string) > $size && substr($string,-3) !== '...')

One observation to make, is that in the example above, will check whether the variable size string is greater than the value reported in the variable $size, if it is the condition will do the check if the variable string does not contain the three points at the end, returning true ( that does not contain the three points ) it will limit the variable string, if contain it will not limit.

See working on ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.