English 中文(简体)
substr strpos帮助
原标题:substr strpos help
  • 时间:2011-02-12 22:03:40
  •  标签:
  • php
  • string

下面的脚本提取$description中的内容摘要,如果在前50个字符中找到句点,则返回前50个字和句点。然而,缺陷在于,当内容中不存在句点时,它只返回第一个字符。

function get_cat_desc($description){
    $the_description = strip_tags($description);
    if(strlen($the_description) > 50 ) 
        return SUBSTR( $the_description,0,STRPOS( $the_description,".",50)+1);
        else return  $the_description;}

我想这样做,如果没有找到句点,它会一直返回到50个字符后的第一个空格(这样它就不会截断一个单词),并附加“…”

最佳回答

最好使用正则表达式。这将匹配$description到$maxLength(函数中的第二个参数),但将一直持续到找到下一个空格为止。

function get_cat_desc($description, $max_length = 50) {
    $the_description = strip_tags($description);
    if(strlen($the_description) > $max_length && preg_match( #^s*(.{ . $max_length . ,}?)[,.s]+.*$#s , $the_description, $matches)) {
        return $matches[1] . ... ;
    } else {
        return $the_description;
    }
}
问题回答

我认为它只需要稍微复杂一点:

function get_cat_desc($description){
    $the_description = strip_tags($description);
    if(strlen($the_description) > 50 ) {
        if (STRPOS( $the_description,".",50) !== false) {
            return SUBSTR( $the_description,0,STRPOS( $the_description,".",50)+1);
        } else {
            return SUBSTR( $the_description,0,50) .  ... ;
        }
    } else {
        return  $the_description;
    }
}

试试这样的方法:

$pos_period = strpos($the_description,  . );
if ($pos_period !== false && $pos_period <= 50) {
    return substr($the_description, 0, 50);
} else {
    $next_space = strpos($the_description,    , 50);
    if ($next_space !== false) {
        return substr($the_description, 0, $next_space) .  ... ;
    } else {
        return substr($the_description, 0, 50) .  ... ;
    }
}

使用substra_count查找它,然后执行substr(,0,50)





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签