2012-01-24 3 views
3

私はメインの索引で私のPHPのウェブサイト(Wordpressサイトではない)に取り組んでいます。私は2つの最新の投稿を表示します。物事は説明全体に記事全体を表示しています。記事の抜粋を表示するには35語の制限が必要です。単語の数によって制限された投稿の抜粋を表示

<?=$line["m_description"]?> 

<? 
$qresult3 = mysql_query("SELECT * FROM t_users WHERE u_id=".$line["m_userid"]." LIMIT 1"); 
if (mysql_num_rows($qresult3)<1) { ?> 
+1

に句読点や数字を占めあなたの質問は何ですか?あなたは記事の35語だけを表示する方法を知りたいですか? –

+0

はい記事の35語しか表示しないでください。 – user1167384

+0

[PHPでsubstr()を使って完全な単語をキャプチャする方法、単語単位で制限する方法?](http://stackoverflow.com/questions/3538130/how-to-capture-complete-words-using-substr-in -php-limit-by-word) – rdlowrey

答えて

0

私は他の人が(ヒントは、人々を歓迎)あまりにも、私はまだPHPが得意だから、それはよくないと言うかもしれないが、これはあなたが探しているもの、あなたを与えるだろうが、それがあれば、より良いコーディングを必要とするかもしれない機能を持っています誰にでも提案があります。

function Short($text, $length, $url, $more){ 
$short = mb_substr($text, 0, $length); 

if($short != $text) { 
    $lastspace = strrpos($short, ' '); 
    $short = substr($short , 0, $lastspace); 

    if(!$more){ 
     $more = "Read Full Post"; 
    } // end if more is blank 

    $short .= "...[<a href='$url'>$more</a>]"; 
} // end if content != short 

$short = str_replace("’","'", $short); 
$short = stripslashes($short); 
$short = nl2br($short); 

} // end short function 

使用するには:

は、あなたの記事の内容は同様に、あなただけの$のURLを削除する機能を調整し、$よりそれからとすることができます変数$コンテンツ

function($content, "35", "http://domain.com/article_post", "Read Full Story"); 
echo $short; 

であると言います最後に...との抜粋があります。

+0

のような? (編集) – bowlerae

+0

@bowleraeこれは素晴らしい出発点のようです。あなたがこのようなファンキーなウィンドウの文字(ハイフン、スマート/二重引用符、ダッシュ、コピーシンボルなど)を整理したい場合は、単引用符よりも多くのシンボルがありますが、それ自体が全体的な話題です。また、抜粋ではほとんどの場合htmlに問題があり、最終的には切り捨てられます。それも解決してください! :) – Kato

9
<?php 

// just the excerpt 
function first_n_words($text, $number_of_words) { 
    // Where excerpts are concerned, HTML tends to behave 
    // like the proverbial ogre in the china shop, so best to strip that 
    $text = strip_tags($text); 

    // \w[\w'-]* allows for any word character (a-zA-Z0-9_) and also contractions 
    // and hyphenated words like 'range-finder' or "it's" 
    // the /s flags means that . matches \n, so this can match multiple lines 
    $text = preg_replace("/^\W*((\w[\w'-]*\b\W*){1,$number_of_words}).*/ms", '\\1', $text); 

    // strip out newline characters from our excerpt 
    return str_replace("\n", "", $text); 
} 

// excerpt plus link if shortened 
function truncate_to_n_words($text, $number_of_words, $url, $readmore = 'read more') { 
    $text = strip_tags($text); 
    $excerpt = first_n_words($text, $number_of_words); 
    // we can't just look at the length or try == because we strip carriage returns 
    if(str_word_count($text) !== str_word_count($excerpt)) { 
     $excerpt .= '... <a href="'.$url.'">'.$readmore.'</a>'; 
    } 
    return $excerpt; 
} 

$src = <<<EOF 
    <b>My cool story</b> 
    <p>Here it is. It's really cool. I like it. I like lots of stuff.</p> 
    <p>I also like to read and write and carry on forever</p> 
EOF; 

echo first_n_words($src, 10); 

echo "\n\n-----------------------------\n\n"; 

echo truncate_to_n_words($src, 10, 'http://www.google.com'); 

EDIT:追加機能の例とテキスト

関連する問題