67

我知道如何使用 substr 函数,但这会在一个单词的中途结束一个字符串。我希望字符串在一个单词的末尾结束,我该怎么做呢?它会涉及正则表达式吗?非常感谢任何帮助。

这就是我到目前为止所拥有的。只是 SubStr...

echo substr("$body",0,260);

干杯

4

11 回答 11

142
substr($body, 0, strpos($body, ' ', 260))
于 2012-06-02T18:16:43.367 回答
114

可以用正则表达式来完成,这样的事情从字符串的开头到单词边界最多可以有 260 个字符:

$line=$body;
if (preg_match('/^.{1,260}\b/s', $body, $match))
{
    $line=$match[0];
}

或者,您可以使用wordwrap函数将您的 $body 分成几行,然后只提取第一行。

于 2009-08-05T13:41:05.460 回答
38

你可以试试这个:

   $s = substr($string, 0, 261);
   $result = substr($s, 0, strrpos($s, ' '));
于 2009-08-05T13:45:46.123 回答
13

你可以这样做:从第 260 个字符开始找到第一个空格并将其用作裁剪标记:

$pos = strpos($body, ' ', 260);
if ($pos !== false) {
    echo substr($body, 0, $pos);
}
于 2009-08-05T13:47:06.513 回答
5

wordwrap 和 explode 然后第一个数组元素是你想要的 $wr=wordwrap($text,20,':'); $strs=explode(":",$wr); $strs[0]

于 2016-11-05T10:35:05.853 回答
1

我使用这个解决方案:

$maxlength = 50;
substr($name, 0, ($spos = strpos($name, ' ', $lcount = count($name) > $maxlength ? $lcount : $maxlength)) ? $spos : $lcount );

或内联:

substr($name, 0, ($spos = strpos($name, ' ', $lcount = count($name) > 50 ? $lcount : 50)) ? $spos : $lcount );
于 2013-10-29T13:24:55.807 回答
1
function substr_word($body,$maxlength){
    if (strlen($body)<$maxlength) return $body;
    $body = substr($body, 0, $maxlength);
    $rpos = strrpos($body,' ');
    if ($rpos>0) $body = substr($body, 0, $rpos);
    return $body;
}
于 2013-12-17T01:13:44.477 回答
1

那这个呢?

/**
 * @param string $text
 * @param int $limit
 * @return string
 */
public function extractUncutPhrase($text, $limit)
{
    $delimiters = [',',' '];
    $marks = ['!','?','.'];

    $phrase = substr($text, 0, $limit);
    $nextSymbol = substr($text, $limit, 1);


    // Equal to original
    if ($phrase == $text) {
        return $phrase;
    }
    // If ends with delimiter
    if (in_array($nextSymbol, $delimiters)) {
        return $phrase;
    }
    // If ends with mark
    if (in_array($nextSymbol, $marks)) {
        return $phrase.$nextSymbol;
    }

    $parts = explode(' ', $phrase);
    array_pop($parts);

    return implode(' ', $parts); // Additioanally you may add ' ...' here.
}

测试:

public function testExtractUncutPhrase()
{
    $stringUtils = new StringUtils();

    $text = 'infant ton-gue could make of both names nothing';
    $phrase = 'infant';

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 11));
    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 12));

    $text = 'infant tongue5';
    $phrase = 'infant';

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 13));
    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 11));
    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 7));
}

public function testExtractUncutPhraseEndsWithDelimiter()
{
    $stringUtils = new StringUtils();

    $text = 'infant tongue ';
    $phrase = 'infant tongue';

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 13));

    $text = 'infant tongue,';
    $phrase = 'infant tongue';

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 13));
}

public function testExtractUncutPhraseIsSentence()
{
    $stringUtils = new StringUtils();

    $text = 'infant tongue!';
    $phrase = 'infant tongue!';

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 14));
    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 100));

    $text = 'infant tongue!';
    $phrase = 'infant tongue!';

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 13));

    $text = 'infant tongue.';
    $phrase = 'infant tongue.';

    $this->assertEquals($phrase, $stringUtils->extractUncutPhrase($text, 13));
}
于 2018-03-09T11:00:00.203 回答
0
$pos = strpos($body, $wordfind);
echo substr($body,0, (($pos)?$pos:260));
于 2009-08-05T13:47:47.760 回答
-1
public function Strip_text($data, $size, $lastString = ""){
    $data = strip_tags($data);          
    if(mb_strlen($data, 'utf-8') > $size){
        $result = mb_substr($data,0,mb_strpos($data,' ',$size,'utf-8'),'utf-8');
            if(mb_strlen($result, 'utf-8') <= 0){
            $result = mb_substr($data,0,$size,'utf-8');
            $result = mb_substr($result, 0, mb_strrpos($result, ' ','utf-8'),'utf-8');;         
        }
        if(strlen($lastString) > 0) {
            $result .= $lastString;
        }
    }else{
    $result = $data;
    }
    return $result; 
}

将字符串传递给函数Strip_text("Long text with html tag or without html tag", 15) 然后此函数将返回不带 html 标签的 html 字符串的前 15 个字符。当字符串少于 15 个字符时,则返回完整的字符串,否则将返回 15 个字符和 $lastString 参数字符串。

例子:

Strip_text("<p>vijayDhanasekaran</p>", 5)

结果:维杰

Strip_text("<h1>vijayDhanasekaran<h1>",5,"***....")

结果:vijay***....

于 2014-06-14T10:58:40.263 回答
-2

试试这个功能..

<?php
/**
 * trims text to a space then adds ellipses if desired
 * @param string $input text to trim
 * @param int $length in characters to trim to
 * @param bool $ellipses if ellipses (...) are to be added
 * @param bool $strip_html if html tags are to be stripped
 * @param bool $strip_style if css style are to be stripped
 * @return string
 */
function trim_text($input, $length, $ellipses = true, $strip_tag = true,$strip_style = true) {
    //strip tags, if desired
    if ($strip_tag) {
        $input = strip_tags($input);
    }

    //strip tags, if desired
    if ($strip_style) {
        $input = preg_replace('/(<[^>]+) style=".*?"/i', '$1',$input);
    }

    if($length=='full')
    {

        $trimmed_text=$input;

    }
    else
    {
        //no need to trim, already shorter than trim length
        if (strlen($input) <= $length) {
        return $input;
        }

        //find last space within length
        $last_space = strrpos(substr($input, 0, $length), ' ');
        $trimmed_text = substr($input, 0, $last_space);

        //add ellipses (...)
        if ($ellipses) {
        $trimmed_text .= '...';
        }       
    }

    return $trimmed_text;
}
?>
于 2014-09-29T11:33:48.497 回答