2011-04-19 19 views
6

मेरे पास एक PHP चर में एक बड़ा HTML कोड है:PHP: एचटीएमएल के पहले 500 अक्षर प्रदर्शित करें

$html_code = '<div class="contianer" style="text-align:center;">The Sameple text.</div><br><span>Another sample text.</span>....'; 

मैं इस कोड के केवल पहले 500 वर्ण प्रदर्शित करना चाहता हूं। इस चरित्र गणना को HTML टैग में टेक्स्ट पर विचार करना चाहिए और लंबाई को मापते समय HTMl टैग और विशेषताओं को बहिष्कृत करना चाहिए। लेकिन कोड को ट्रिम करते समय, इसे HTML कोड की DOM संरचना को प्रभावित नहीं करना चाहिए।

क्या कोई ट्यूमरियल या काम करने वाले उदाहरण उपलब्ध हैं?

+0

क्या आप पाठ को HTML के बिना गिना जाना चाहते थे, लेकिन फिर मूल HTML में लपेटा गया? –

+0

हां। मैं चाहता हूं कि इसे HTML में लपेटा जाए ताकि मैं इसे किसी पृष्ठ पर प्रदर्शित कर सकूं। – Vin

+0

मैंने अपने पिछले उत्तर में कुछ छद्म जोड़ा है - उम्मीद है कि यह आपको सही दिशा में इंगित कर सकता है। मुझे सचमुच खेद है कि मुझे इसे सही बनाने के लिए काम करने का समय नहीं मिला है, लेकिन उंगलियों को पार करने से आपको बाधा मिल जाएगी। अगर आप समाधान के साथ आते हैं तो अपना जवाब पोस्ट करें! मुझे देखना अच्छा लगेगा। –

उत्तर

3

Ooohh ... मैं इस मैं यह वास्तव में मेरे सिर के ऊपर से नहीं मिल सकता है पता है, लेकिन आप

http://www.php.net/manual/en/class.domdocument.php

तो हड़पने पाठ आप एक DOMDocument के रूप में मिल गया है लोड करना चाहते हैं पूरे दस्तावेज़ नोड से टेक्स्ट (एक डोमोडेड http://www.php.net/manual/en/class.domnode.php के रूप में)

यह बिल्कुल सही नहीं होगा, लेकिन उम्मीद है कि यह आपको सही रास्ते पर ले जाएगा। की तरह कुछ का प्रयास करें:

$html_code = '<div class="contianer" style="text-align:center;">The Sameple text.</div><br><span>Another sample text.</span>....'; 
$dom = new DOMDocument(); 
$dom->loadHTML($html_code); 
$text_to_strip = $dom->textContent; 
$stripped = mb_substr($text_to_strip,0,500); 
echo "$stripped"; // The Sameple text.Another sample text..... 

संपादित ठीक है ... कि काम करना चाहिए। सिर्फ स्थानीय स्तर पर

EDIT2

परीक्षण किया अब मैं समझता हूँ कि आप टैग रखने के लिए, लेकिन पाठ को सीमित करना चाहते नहीं, देखें। जब तक आप 500 वर्ण प्राप्त नहीं कर लेते हैं तब तक आप सामग्री को लूप करना चाहते हैं। यह शायद कुछ संपादन करने जा रहा है और मेरे लिए सही होने के लिए गुजरता है, लेकिन उम्मीद है कि मैं मदद कर सकता हूं। (खेद है कि मैं अविभाजित ध्यान नहीं दे सकता)

पहला मामला तब होता है जब पाठ 500 वर्णों से कम होता है। किसी बारे में चिन्ता की जरूरत नहीं। उपर्युक्त कोड से शुरू करना हम निम्नलिखित कर सकते हैं।

if (strlen($stripped) > 500) { 
     // this is where we do our work. 

     $characters_so_far = 0; 
     foreach ($dom->child_nodes as $ChildNode) { 

      // should check if $ChildNode->hasChildNodes(); 
      // probably put some of this stuff into a function 
      $characters_in_next_node += str_len($ChildNode->textcontent); 
      if ($characters_so_far+$characters_in_next_node > 500) { 
       // remove the node 
       // try using 
       // $ChildNode->parentNode->removeChild($ChildNode); 
      } 
      $characters_so_far += $characters_in_next_node 
     } 
     // 
     $final_out = $dom->saveHTML(); 
    } else { 
     $final_out = $html_code; 
    } 
+0

'टेक्स्ट सामग्री' एक संपत्ति है। इसके अलावा, क्या यह सभी एचटीएमएल छीन नहीं होगा? – alex

4

अपने पाठ आप चाहते हैं, आप भी निम्न

substr(strip_tags($html_code),0,500); 
+0

मुझे HTML प्रारूप में आउटपुट स्ट्रिंग की आवश्यकता है। इसलिए मैं इसे HTML पृष्ठ पर प्रदर्शित कर सकता हूं। – Vin

1

मैं एक असली समाधान कोडिंग करने के लिए नहीं कर रहा हूँ, लेकिन अगर किसी को चाहता है, यहाँ क्या करने के लिए के साथ ऐसा कर सकते हैं मैं (छद्म PHP में) करना चाहते हैं:

$html_code = '<div class="contianer" style="text-align:center;">The Sameple text.</div><br><span>Another sample text.</span>....'; 
$aggregate = ''; 

$document = XMLParser($html_code); 

foreach ($document->getElementsByTagName('*') as $element) { 
    $aggregate .= $element->text(); // This is the text, not HTML. It doesn't 
            // include the children, only the text 
            // directly in the tag. 
} 
2

मैं एक php वर्ग नीचे चिपकाने कर रहा हूँ मैं एक लंबे समय पहले लिखा था, लेकिन मैं यह काम करता है पता है। यह ठीक नहीं है कि आप क्या कर रहे हैं, क्योंकि यह एक चरित्र गणना के बजाय शब्दों से संबंधित है, लेकिन मुझे इसकी सुंदर नज़दीकी आकृति है और किसी को यह उपयोगी लगेगा।

class HtmlWordManipulator 
    { 
    var $stack = array(); 

    function truncate($text, $num=50) 
    { 
     if (preg_match_all('/\s+/', $text, $junk) <= $num) return $text; 
     $text = preg_replace_callback('/(<\/?[^>]+\s+[^>]*>)/','_truncateProtect', $text); 
     $words = 0; 
     $out = array(); 
     $text = str_replace('<',' <',str_replace('>','> ',$text)); 
     $toks = preg_split('/\s+/', $text); 
     foreach ($toks as $tok) 
     { 
     if (preg_match_all('/<(\/?[^\x01>]+)([^>]*)>/',$tok,$matches,PREG_SET_ORDER)) 
      foreach ($matches as $tag) $this->_recordTag($tag[1], $tag[2]); 
     $out[] = trim($tok); 
     if (! preg_match('/^(<[^>]+>)+$/', $tok)) 
     { 
      if (!strpos($tok,'=') && !strpos($tok,'<') && strlen(trim(strip_tags($tok))) > 0) 
      { 
      ++$words; 
      } 
      else 
      {     
      /* 
      echo '<hr />'; 
      echo htmlentities('failed: '.$tok).'<br /)>'; 
      echo htmlentities('has equals: '.strpos($tok,'=')).'<br />'; 
      echo htmlentities('has greater than: '.strpos($tok,'<')).'<br />'; 
      echo htmlentities('strip tags: '.strip_tags($tok)).'<br />'; 
      echo str_word_count($text); 
      */ 
      } 
     } 
     if ($words > $num) break; 
     } 
     $truncate = $this->_truncateRestore(implode(' ', $out)); 
     return $truncate; 
    } 

    function restoreTags($text) 
    { 
     foreach ($this->stack as $tag) $text .= "</$tag>"; 
     return $text; 
    } 

    private function _truncateProtect($match) 
    { 
     return preg_replace('/\s/', "\x01", $match[0]); 
    } 

    private function _truncateRestore($strings) 
    { 
     return preg_replace('/\x01/', ' ', $strings); 
    } 

    private function _recordTag($tag, $args) 
    { 
     // XHTML 
     if (strlen($args) and $args[strlen($args) - 1] == '/') return; 
     else if ($tag[0] == '/') 
     { 
     $tag = substr($tag, 1); 
     for ($i=count($this->stack) -1; $i >= 0; $i--) { 
     if ($this->stack[$i] == $tag) { 
      array_splice($this->stack, $i, 1); 
      return; 
     } 
     } 
     return; 
     } 
     else if (in_array($tag, array('p', 'li', 'ul', 'ol', 'div', 'span', 'a'))) 
     $this->stack[] = $tag; 
     else return; 
    } 
    } 

truncate आप क्या चाहते है, और आप इसे एचटीएमएल और शब्द आप चाहते हैं यह करने के लिए कांट छांट की संख्या गुजरती हैं। यह शब्दों की गिनती करते समय एचटीएमएल को अनदेखा करता है, लेकिन फिर छेड़छाड़ के कारण एचटीएमएल में सब कुछ दोहराता है, यहां तक ​​कि पिछला टैग बंद कर देता है।

कृपया ओओपी सिद्धांतों की पूरी कमी पर मेरा फैसला न करें। मैं युवा और बेवकूफ था।

संपादित करें:

तो यह पता चला है उपयोग अधिक इस तरह है:

$content = $manipulator->restoreTags($manipulator->truncate($myHtml,$numOfWords)); 

बेवकूफ डिजाइन निर्णय। हालांकि मुझे अनजान टैग के अंदर एचटीएमएल इंजेक्ट करने की अनुमति दी।

संबंधित मुद्दे