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!!
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!