I am useing Zend\Dom\Query to get specific content from a webpage.
This is the html:
<div class="menuName darkGreen leftMenu">
Mouse
</div>
How can I get the value Mouse ??
Thanks
Try this:
$dom = new \Zend\Dom\Query($html);
$results = $dom->execute('div.menuName a');
$doc = $results->current();
var_dump($doc->nodeValue);
Try this:
$doc->getAttribute('src');
Related
How can I apply a tailwind class, if another var is true ... the follwoing does not seem to work.
This works
<script>
let svgColor = 'text-red-400'
</script>
<p class="{svgColor}">Test</p>
But this does not
<script>
let svgColor = 'text-red-400'
let svgVisible = true;
</script>
<p class="{svgVisible ? {svgColor} : ''}">Test</p>
By wrapping the svgColor with the curly braces you are effectively making an object with the prop svgColor.
The correct syntax would be:
<p class={svgVisible ? svgColor : ''}>Test</p>
I spent so much time on a very simple thing and had to post here on StackOverflow
I want to get all inner text except the script/style tags
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$html = <<<EOD
<div>
<script>var main=0</script>
<div>
<p>my</p>
<script>var inner=0</script>
</div>
<p>text</p>
only
</div>
EOD;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
echo $entries = $xpath->query('//*[not(self::script)]')->item(0)->nodeValue;
gives me
var main=0 my var inner=0 text only
and also tried
$entries = $xpath->query('//*[not(self::script)]');
foreach ($entries as $entry) {
if ($entry->tagName == 'style' || $entry->tagName == 'script') {
continue;
}
echo preg_replace('/\s\s+/', ' ', $entry->nodeValue);
}
gives me
var main=0 my var inner=0 text only var main=0 my var inner=0 text only var main=0 my var inner=0 text only my var inner=0mytext
I tried several xpaths but it doesn't work
my desired output is my text only
I am a Scrapy developer and I do that easily in Scrapy, but having a bad time with PHP today
Unfortunately, PHP doesn't support xpath 2.0 (and, IIRC, neither does Scrapy), so the name() method which would have made it easy, isn't available...
The closest thing I can think of is the following, which should get you close enough (note that, because there is no <style> tag in your $html, I only focused on <script>):
$entries = $xpath->query('//*[not(./text()/parent::script)]/text()');
foreach ($entries as $entry) {
echo trim($entry->textContent) . " ";
}
Output:
my text only
I am returning this from a partial view:
<script>
document.getElementById("login").style.visibility = "hidden";
document.getElementById("displayname").innerHTML = #ViewBag.DisplayName
</script>
The second script line, however, does not work.
How to write this correctly?
Greg
#Html.Raw("document.getElementById(\"displayname\").innerHTML = " + #ViewBag.DisplayName)
Is it possible to make LIElement into a link, and how can it be done?
I have tried
var li = new LIElement();
li.text = text;
li.href = "http://www.google.com";
But it does not seem to work.
Any help would be greatly appreciated.
You have to add an anchor element, just like you would do it in HTML.
LIElement li = new LIElement();
AnchorElement a = new AnchorElement();
a.text = "I'm a <li> with an <a>!";
a.href = "http://www.google.com";
li.append(a);
There's no href setter on LIElement. Alternativly, you can add a listener on click to change the location of the page.
var li = new LIElement();
li.text = text;
li.onClick.listen((e) => window.location.assign("http://www.google.com"));
Is there anything I can use instead of 'a' selector. Maybe something like <url href=""> or something similar. I'm trying to avoid 'a'. The reason for that is that I'm working with jQuery and its code modyfying every 'a' in the section. I want to keep some 'a's untouched. This is what i have:
HTML:
<div> <!-- █ POST █ -->
<h3>14 June 2012 - Noisettes for Spotify</h3>
<p>
We have supplied a sound equipment for the Noisettes concert on Spotify company event. Please enjoy <a class="inlink" target="_blank" href="http://www.youtube.com/watch?v=KRFHiBW9RE8">Noisettes on YouTube</a>.
</p>
</div>
JQUERY:
$(document).ready(function(){
var $posts = $('#items > div');
var maxHeight = 32;
$('a', $posts).live('click', function(){
var $par = $(this).prev('p');
var oH = parseInt($par.attr('class'));
if($par.height() == oH){
$par.animate({
'height': maxHeight
}, 'medium');
$(this).text('read more...');
}else{
$par.animate({
'height': oH
}, 'medium');
$(this).text('read less...');
}
});
$posts.each(function(){
if($('p', this).height() > maxHeight){
$('p', this)
.attr('class', $('p', this).height())
.css('height', maxHeight+'px');
$(this).append($('<a class="link">').text('read more...'));
}
});
});
It replaces my 'Noisettes on YouTube' (from html code) with 'read less...' (still working but wording changes. I was trying to use CSS but it still replaces it.
I hope I made myself clear on this :) Thanks for help in advance.
You should use a more specific selector:
$posts.find('a.SomeClass')...
Then change the links that you want this to apply to to <a class="SomeClass">.
Give all the "a" that you want to change a particular class, say "changeable", and then change your selector from $('a', $posts) to $('a.changeable', $posts)