2011-08-05 9 views
0

から来ているときに、私はすでにTinyMCEを使用して設定されているコンテンツのトンを持っていると私は何とかのxの文字数または最初<p>タグを取得する必要があり、その後「続きを読む」を追加続きを読みます残りのコンテンツを表示するリンクをクリックします。私はオンラインで見ましたが、うまくいかないものは見つけられないようです。そして、私が見つけたものは、文字列のすべてのhtmlを取り除くだけで本当にうまく動作します。ダイナミックを作成する情報は、WYSIWYG

誰にもアイデアはありますか?

+0

あなたが使用していますCMSやフレームワークの何か?またはこれはちょうどPHPですか?コンテンツは(データベース、PHPファイル、他の何か...)どこから来ている – tmsimont

+0

は、それがこれを行うには難しいことが判明しました。 – tmsimont

答えて

0

はのは$xあなたはWYSIWYGから受信したコンテンツであるとしましょう。

echo substr($x, 0, 100) . '... <a href="[your post url here]">Read More</a>'; 

これは$xの最初の100個の文字をエコーし​​、最後まで読んでより多くのリンクを連結します。 希望すると、thisもチェックしてください。

+0

私はそれを試してみましたが、問題がクローズされていないHTMLは、時間の前に存在している場合は100文字物事をめちゃくちゃにしてしまうことがあります。 – rchiriboga

+0

私は、あなたが最初のHTMLタグがコンテンツであるものは何でも読み、最後にそれを連結する機能を構築したいことがあり、参照してください。 – k4t434sis

1

私はちょっとこれは私が思った以上に複雑であることが判明したとして、最初のコードととりとめで答え...しかし、ここで私が前に言ったことのより一貫性バージョンです:)

私の提案した解決策があります元の文字列に0〜100の文字の部分文字列を取ります。これは危険な文字列で、終端されていないHTMLタグを含む可能性があります。このページ

/**  
* 1. Loop from char 0 to 100, at each character, check if 
* it might be an open tag "<" 
* 2. Check if it's a "<TAG>" or "</TAG>" 
* 3. Each time "<TAG>" occurs, add to array of 
* "tags to close" Strings 
* 4. Each time "</TAG>" occurs, pop the last entered "<TAG>" 
* from the array 
* 5. Once all the string is examined, foreach "tags to close", 
* write "</".TAG.">" 
**/ 

作業例:

次に、この刻んだ文字列(これは実際には文字列をカットしなかった場合、その後の処理にはポイントがありません)入力文字列よりも短い場合にのみ、魔法を開始:部分的にしか解決されConcatenate a string, and terminate html within

可能性のある問題(track_tag()if(!$endloop){セクションを参照してください):

  1. 最後のH TMLタグは、最初にsubstr()メソッドで切り刻まれています。入力されたHTML文字列は、例えば、有効なHTMLではありません
  2. ...など、<diまたは<divまたは</h:たとえば、文字列がで終わります<div><h1>invalid html</div></h1>または</p><p>what's with the random </p> before this <p>, man?</p>

Unforutnately私は入れないだろう、とあなたはWYSIWYGに生成された文字列でそれらを参照してくださいする可能性があるワームの全体の缶を開く...

<?php 
    $killer = new tagEndingConcatMachine(); 
    $killer->summaryLength = $length; 
    $killer->end = "..."; 
    $killer->chop_and_append($input)."Read more"; 


class tagEndingConcatMachine { 
    public $end = '...'; 
    public $summaryLength = 100; 


    private $tags_to_end = array(); 

    public function chop_and_append($x){ 
     $summary = substr($x, 0 ,$this->summaryLength); 
     if($summary !== $x){ 
      $this->end_tags($summary);  
      return $summary . $this->end; 
     } 
     return $summary; 
    } 

    private function end_tags(&$summary){ ; 
     for($i = 0; $i<=$this->summaryLength; $i++){ 
      if($summary[$i]=='<'){ 
      $this->track_tag($summary, $i); 
      } 
     } 
     for($i = count($this->tags_to_end); $i>=0; $i--){ 
      if($this->tags_to_end != '' && isset($this->tags_to_end[$i])) 
       $this->end .= '</'.$this->tags_to_end[$i].">"; 
     } 
    } 

    private function track_tag(&$summary, $i){ 
     $this_tag = ''; 
     $endloop = false; 
     $ending = false; 
     $k = $i+1; 
     do{ 
      $thischar = $summary[$k]; 
      if($thischar=='/' && $summary[$k-1]== '<'){ 
       $ending = true; 
      }elseif($thischar=='>'){ 
       if($this_tag!=''){ 
        if($ending) 
         array_pop($this->tags_to_end); 
        else 
         $this->tags_to_end[] = strtolower($this_tag); 
       } 
       $endloop = true; 
      }else{ 
       $this_tag .= $thischar; 
      } 
      $k++; 
     }while($k<=$this->summaryLength && !$endloop); 
     if(!$endloop){ 
      if($ending){ 
      //opened end tag, never closed 
       //could be trouble... but tags_to_end knows which to close 
       $this->end = '>'.$this->end; 
      }else{ 
      //open opening tag... remove it from the end of the summary 
       $summary = substr($summary, 0, strlen($summary)-strlen($this_tag)-1); 
      } 
     } 
    } 
} 
?> 
+0

私は今週末にあなたのクラスを試してみると思います。クライアントは「もっと読む前にもっと多くのコンテンツを見ることはできますか」と戻ってきました。このTmsimontに感謝します。 – rchiriboga

+0

これは楽しいパズルだったと確信しています - あなたが助けが必要な場合はhttp://www.trevorsimonton.com/を介して私にメールを送りなさい – tmsimont

関連する問題