简体   繁体   中英

How can we limit a string to a given number of characters in Php

我们如何在 Php 中将字符串限制为给定数量的字符?

Dirty. For example only:

function contentPreview($inputString, $limit = 200)
{
    $inputString = strip_tags($inputString);
    $inputString = preg_replace('/\s+| /', ' ', $inputString);
    return preg_match('#^(.{' . $limit . ',}?)\s+#su', $inputString, $match)
        ? $match[1] . '...' : $inputString;
}

Why not use default built in StringHelper ? no need to create custom functions

StringHelper::truncate($mycontent,100,"...")

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