I need to get some part of html pages using php XPath query (sorry im new to XPath).
example html page
<html><head>blah lines</head><body>
<div id="mytable">
<table><tr><td>table cell value</td></tr></table>
</div>
<body><html>
is there any way to get div contents of above example along with html tags using XPath
The following xpath query will return the "mytable" div:
//div[#id='mytable']
//div will select all divs in the document
[#id='mytable'] will return only matches with a "mytable" attribute
/html/body/div[#id='mytable']
thank you all, but i was need whole part including tags, so i solved as follows
$detail= $xpath->query("//div [#id='mytable']");
$querycontent = $detail->item(0);
$html->saveXML($querycontent);
Related
On a PHP+MySQL project, there's a string of text coming from a MySQL table that contains HTML tags but those tags never get rendered by Google Chrome or any browser I tried yet:
You can see that the HTML (p, strong) aren't getting interpreted by the browser.
So the result is:
EDIT: HTML/PHP
<div class="winery_description">
<?php echo $this->winery['description']; ?>
</div>
$this->winery being the array result of the SQL Select.
EDIT 2: I'm the dumbest man in the world, the source contains entities. So the new question is: How do I force entities to be interpreted?
Real source:
Any suggestions? Thanks!
You are probably using innerText or textContent to set the content of your div, which just replace the child nodes of the div with a single text node.
Use innerHTML instead, to have the browser parse the HTML and create the appropriate DOM nodes.
The answer provided by #Paulpro is correct.
Also note that if you are using jQuery, be sure to use the .html() method instead of .text() method:
$('#your_element').html('<h1>This works!</h1>');
$('#another_element').text('<h2>Wrong; you will see the <h2> in the output');
I'm using XPath to get div contenteditable's content and insert it in DB. But when I output the content there are no new lines.
$divs = $xpath->query('//div');
foreach($divs as $book) {
$text = nl2br($book->textContent);
}
What am I doing wrong? Thanks in advance :)
$book->textContent strips all markup from the content [1], such as <div> which are naturally inserted when you hit enter on an contenteditable-Element. Therefore all this function returns is a concatenation of pure text.
I'm not sure what your goal is, but you might look into $your_DOMDocument->saveHTML($book). This will return the actual HTML of each $book including the surrounding <div>.
[1]: see the DOM Specifications
You might be able to set a xml:whitespace="preserve" attribute on the element that has content editable and it come through using the xml api.
Failing this a solution would be to add a keypress handler to the content editable div that catches an enter and replaces it with a <br/> at the cursor position.
I have html text stored in one of the columns of a database. the column name is mailbody and the table name is inbox_master.
Some the cells of column mailbody has divs like below
<div id="uid-g-uid" style="">2802</div>
or
<div id="uid-g-uid">
<p class="MsoNormal">6894</p>
</div>
or
<div id="uid-g-uid" style="display:none;">
6894</div>
what is common here is a div with the id "uid-g-uid". I want to be able to read the html of this div. I know this could be done using regular expressions however, not sure how to do it.
Below is the regex that i have tried but doesnt work all the time
/(?<=\<div\ id\=\"uid\-g\-uid\").*?(?=\<\/div\>)/gim
Thanks to #sikfire and #dave, i got the solution using DOM. below is my working which helped me
$doc = new DOMDocument();
#$doc->loadHTML('The HTML Content Goes here');
$d = $doc->getElementById('uid-g-uid');
echo 'Value is ' . $d['textContent'];
Didnt knew this could be this simple! Thanks Guys!
you can also look at the project here. PHP DomParser
This might help!
I'm trying to get the title from a link - which I already have identified, it is just the last bit with retrieving the title from the same link.
To find the right link I use this code:
$html->find('a[href=http://mylink.se']');
But I also want the title from this link. How would I do that?
Assuming you're using PHP Simple HTML DOM parser, which is not clear in your question, you could do
$link = $html->find('a[href=http://mylink.se]', 0); //As the OP pointed out in comments, you need to select the first element
$title = $link->title
http://simplehtmldom.sourceforge.net/manual.htm
I'm trying to insert THE PHP CODE between the <div id="container"> THE PHP CODE HERE </div> . I'm using the http://simplehtmldom.sourceforge.net/ in this Php Html Parser.
I've got this so far, how to find a div having an id called container:
// Find all which attribute id=foo
$ret = $html->find('div[id=foo]');
Then after finding the foo, what's next?
$html->find('div[id=foo]')->innertext = 'bar';
Should do the job but you might be better off modifying the DOM with JS?
Why not modify the source Wordpress template instead of trying to hack in the text later? That PHP snippet could potentially produce a huge chunk of text, and you can't insert it verbatim into the DOM - you can only insert its output (which you'd have to capture with ob_start()/ob_get_clean() anywyas).