douzhu6149 2013-04-22 01:27
浏览 30
已采纳

来自Wordpress Div的推文

I'm trying to pull text from a div created by the "Quotes Collection" plugin/widget in Wordpress in order allow users to tweet it out.

I want to autopopulate the text so I have to grab it.

The code for where the text is located:

    <div class="quote">
        <div id="quotescollection_randomquote-0" class="quotescollection_randomquote">
          <p><q>QUOTE TEXT HERE!</q> </p>
        </div>
    </div>

I'm trying to get that quote text here out and into a prepopulated tweet. I'm a real neophyte with PHP and how this integrates - I think the likely solution is with jquery and .text() and then trying to append that into the twitter href?

Thanks for any help!!

  • 写回答

1条回答 默认 最新

  • dougua2309 2013-04-22 01:32
    关注

    Before start coding you should learn about DOM and XPath.


    In PHP you'll do it using DOMDocument and DOMXPath. Here comes an example:

    $html = <<<EOF
    <div class="quote">
      <div id="quotescollection_randomquote-0" class="quotescollection_randomquote">
        <p><q>QUOTE TEXT HERE!</q> </p>
      </div>
    </div>
    EOF;
    
    // create a document object from your html string
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    
    // create a xpath selector for the document
    $selector = new DOMXPath($doc);
    
    // use xpath query to access the text of <q> node
    $quote = $selector
      ->query('//div[@class="quote"]/div/p/q/text()')
      ->item(0)
      ->nodeValue;
    
    // output
    echo $quote; // Output: QUOTE TEXT HERE!
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改