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

0
How can I put a condition in if ( if (strlen($string) > $size) { ) so that it does not 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 above function is done to reduce the sentence to 30 characters and adds "..." at the end of the sentence. but I need this condition for some already abbreviated sentences that I do not want by the method.

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

    
asked by anonymous 14.09.2017 / 23:01

4 answers

2

You can use PHP's substr function by simply entering a negative number.

Checks the size of the string variable and verifies that the last three characters of the string variable are not the three points.

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

A note to be made is that in the example above, it will check if the size of the variable string is greater than the value entered in the variable $size , if it is the condition it will 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 it contains it will not limit.

See working at ideone

    
15.09.2017 / 01:34
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 have not tested.

    
14.09.2017 / 23:55
2

I do not quite understand the array, but I'll do as I understand:

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

See working at Ideone .

What I did:

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

On the second, it's the same as% w_that you were using, but now it checks to see if the regex returned if .

References:

15.09.2017 / 00:17
2

Try this:

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

What it will do is, look at its string variable if there is anywhere the text "...", if it does not find it, it will execute its code. It searches all $string ignoring the position, seeking only to find what was requested. I think this can solve your problem.

Link for the PHP function. Good luck!

    
15.09.2017 / 18:02