0

我正在尝试将文本从 div 中提取出来,然后发布推文。我正在使用以下方法获取文本,但是当我尝试将其添加到下面的推文链接时:

        <?php 
                    $include = quotescollection_quote();
                    $html = <<<EOF
<div class="quote">

{$include}
                        </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;
 ?>

 <?php echo '<a href="http://twitter.com/home?status=Currently reading' . $quote . 'from the Winifred Paper Blog!" title="Click to send this page to Twitter!" target="_blank">Tweet it</a>' ?>

输出当前是引用变量的内容,作为断开链接的文本。先感谢您!

4

1 回答 1

0

如果您在将 $quote 传递到 URL 时遇到问题,请使用 'urlencode':

 <?php echo '<a href="http://twitter.com/home?status=Currently reading' . urlencode($quote) . 'from the Winifred Paper Blog!" title="Click to send this page to Twitter!" target="_blank">Tweet it</a>' ?>

http://php.net/manual/en/function.urlencode.php

于 2013-04-22T14:58:44.460 回答