来自DOM Selector .text()的PHP变量不会附加到链接

时间:2013-04-22 14:35:33

标签: php wordpress

我正试图将文本从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>' ?>

输出当前是引用变量的内容,因为文本会破坏链接。 先感谢您!

1 个答案:

答案 0 :(得分:0)

如果您将$ quote传递到网址时遇到问题,请使用'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