简体   繁体   中英

Determine last character in PHP string, replacing and returning the string with new characters

I am trying to find the last character in a PHP string consisting of 20 words, trimming and replacing it with "..." only if the last character is one of the following:

, . : ; or a space " "

But if the last character is a:

. ! ?

I would simply like to leave these characters appended to the original string.

How far I've got:

<?php

function limit_words($description, $count) {

    $words = explode(" ", $description);
    $chunk = array_chunk($words, $count);
    $description = implode(" ", $chunk[0]);

    $last_char = $description[strlen($description)-1];
    $replace = "...";

    if ($last_char != "." || "!" || "?") {
        $trim = rtrim($description, $last_char);
        return $trim . $replace;
    }
    else
    {
        return $description;
    }
};

$descrip = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum convallis ipsum ac diam elementum, ac tempus turpis porttitor. Donec eleifend. lobortis gravida. Duis vulputate, ante ac interdum ultricies, elit nunc mollis ante, id consequat nulla mauris vitae nunc. Curabitur leo mauris, tristique in euismod eu, luctus nec libero. Integer sapien erat, egestas ut lectus eu, sagittis scelerisque neque.";
$count = 20;

echo limit_words($descrip, $count);

?>

My code successfully results in a 20 word string, and appends "..." to the end, but you'll notice the last character in $descrip is a . (full-stop), so it should echo "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum convallis ipsum ac diam elementum, ac tempus turpis porttitor. Donec eleifend."

I'd appreciate any help to get this working, hopefully it's just something daft!!

this is wrong

if ($last_char != "." || "!" || "?")

because "!" and "?" are non-zero, so though the first condition is false the next 2 conditions are always true. As a result the whole statement is true.

try

if ($last_char != "." && $last_char!="!" && $last_char!="?") {

you can also do it like this

<?php
function limit_words($description, $count) {

    $words = explode(" ", $description);
    $chunk = array_chunk($words, $count);
    $description = implode(" ", $chunk[0]);

    $last_char = $description[strlen($description)-1];
    $replace = "...";

    // you compare it to an array
    if (!in_array($last_char, array(".", "!", "?"))) {
        $trim = rtrim($description, $last_char);
        return $trim . $replace;
    }
    else
    {
        return $description;
    }
}

$descrip = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum convallis ipsum ac diam elementum, ac tempus turpis porttitor. Donec eleifend. lobortis gravida. Duis vulputate, ante ac interdum ultricies, elit nunc mollis ante, id consequat nulla mauris vitae nunc. Curabitur leo mauris, tristique in euismod eu, luctus nec libero. Integer sapien erat, egestas ut lectus eu, sagittis scelerisque neque.";
$count = 20;

echo limit_words($descrip, $count);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM