Remove the punctuation mark with regular expressio

2019-08-03 23:53发布

问题:

I made this function to limit the length of a string in the output,

/* limit the lenght of the string */
function limit_length($content, $limit)
{
    # strip all the html tags in the content
    $output = strip_tags($content);

    # count the length of the content
    $length = strlen($output); 

    # check if the length of the content is more than the limit
    if ($length > $limit)
    {
        # limit the length of the content in the output
        $output = substr($output,0,$limit);

        $last_space = strrpos($output, ' ');

        # add dots at the end of the output
        $output = substr($output, 0, $last_space).'...';
    }

    # return the result
    return $output;
}

it works fine but I think it is not perfect... for instance, I have this text in the string,

Gender Equality; Radicalisation; Good Governance, Democracy and Human Rights; 

and this is how I use the function,

echo limit_length($item['pg_description'], 20);

then it returns,

Gender Equality;...

As you can it doesn't look great with ;... when you want to tell people that there are more text inside the content/ line.

I was thinking if there is possible to use regular expression to check if there is any punctuation mark present before ... then remove it.

Is it possible? How can I write the expression to improve my function so that can be sort of 'bulletproof'?

Thanks.

回答1:

$str = preg_replace( "/\W+$/", "", $str );


回答2:

To remove anything other than letters directly preceding three periods (adjust as necessary):

$foo = preg_replace("[a-zA-Z0-9]+\.{3}", "...", $foo);