Xpath expression for council website - php

From this URL:
https://www.walsall.gov.uk/Waste/bincollections/Details/100071042591
I want to extract the indivual bin dates from the webpage.
When I inpsect with Chrome, and copy xpath for the first bin date, I get the following XPath:
//*[#id="main"]/table[1]/tbody/tr[2]/td[2]
Which looks good, but I get a length of zero from my $xpath->query object.
However, when I put my query as:
//*[#id="main"]/h3[2]
I get the right value (Green Bin, Recycling).
Can anyone tell me why the first XPath query isnt working? It looks perfectly logical to me.

The problem was with <tbody> tag, I took that out and ended with this:
//*[#id="main"]/table[1]/tr[2]/td[2]
My guess is when I inspected Chrome inserted the tbody tag when you inspect, but it is not actually present in the raw HTML served by the server.

Related

PHP Wordpress: Where does print_r() output to in Safari?

If I execute
print_r('Hello');
on a Wordpress code snippet, where would this actually print? I'm using Safari.
print_r in php typically prints in the parent node of the node you placed the code within.
Since you didnt give more information i assume that
this is the php print_r.
So its most likely that the output will be in the middle of your HTML code or your snippet.
You should find it in the HTML source and maybe also see it on the website

Why is this html code not correctly interpreted?

I am a bit curious about this HTML code (Firebug's view):
<td>1–1 of 1 record found matching your query (RSS | history):</td>
I was trying to install refbase. I used the snapshot version because of SQL problems with the current release. While testing I got this view (Firebug console is opened on purpose to show how the code is interpreted, but I see the same on chromium for instance):
.
.
Then I clicked in the firebug view and added a single space character somewhere, and did escape to let it as it was. But after having added the space, the view changed into this:
.
.
So basically I changed nothing but the code is now correctly interpreted. That's really weird. I googled a bit about this, but I think I might not have found the appropriate keywords.
Could someone explain me the reason behind this weird behaviour and how to fix it? BTW this Here is the complete php source producing this page. Look on line 796.
EDIT:
Thanks to LGSon I noticed that it is a Firebug's trap: it interprets the & stuff to show nice html code and then when you edit-it, it becomes the real code. That's tricky indeed, but when checking page source you can see the real HTML:
<td>1&#8211;1 of 1 record found matching your query (<a href="rss.php?where=title%20RLIKE%20%22physics%22" title="track newly added records matching your current query by subscribing to this RSS feed">RSS</a>&nbsp;|&nbsp;<a href="query_history.php" title="recall a previous query from your current session">history</a>):</td>
It should be 1–1
Sample
1–1

PHP xpath check for div style and its value

I am trying to parse a html file/strings for two things using php and xpath.
<DIV STYLE="top:110px; left:1280px; width:88px" Class="S0">Aug30</DIV>
I tried to look for an unknown value (here: Aug30) with knowing the style top and left value (here: 110px and 1280px).
And the other way. I know the value Aug30 but want to get its values of top and left.
Perhaps XPATH is not the best way to do this. Any idea on how to solve my problem?
Thanks in advance for your help!
To filter <div> element by style attribute value in XPath you can do something like this :
//div[contains(#style, 'top:110px') and contains(#style, 'left:1280px')]
Above XPath will search for <div> node having style attribute value contains two specific strings.
The other requirement isn't supported in XPath 1.0 as far as I can see. We can get the entire value of style attribute, but getting part of it is a dead end. There are some string functions we can use, even though returning a function's result isn't supported.
You'll need to do that using XPath 2.0 or using the host programming language (PHP in this case).

HTML doesn't get rendered, why does it happen?

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');

Find and replace problem

My website, has 2 database tables. 1 of them have the posts_table and the other one have the videos.
At the moment i am getting the text images etc , normally from the post_table table.
In my CMS when we add a video there is added a short code
[media id=487 width=660 height=440]
This shortcode automaticly get the link of a video from the vid_table where the id is the same as the shortcode.
So what i want is:
I need to do the same thing that the short code do, when a video is added on CMS the short code is showed in the post, i need to delete the shortcode and instead of it want to be played a video that has the link on the vid_table.
I have some problems with my english , so if you dont understand again please tell me.
Any kind of help will be great.
Thank you.
EDITED: So i want to replace the whole media tag with a flash player, that plays the url that belongs to the ID in the media tag
BUMP !! CAN HELP PLEASE ?
This is quite a sophisticated problem actually. I was bored and made a basic tag parser. Right now it has some problems:
HTML rendering should be implemented in a separated class (and a template engine such as Twig should do the rendering);
Tag parsing is way too naive and will probably give you unexpected results if a tag's syntax is incorrect;
[media] tag does not support IE. You would have to change the source itself (method TagParser::renderMedia())
Some features to note:
extra parameters will be rendered as attributes for [link] tag, e.g [link id=25 class=foo] will output example.
parameters may contain spaces if you quote them: [link id=25 class="foo bar"] will output example
If DataProvider::findById() does not return a 'content' in its array, the parser will output http://example.com
The code is too long to paste here, you can find it on gist. Just put each file in the directory specified by the first commented line and you should be set. Run example.php to see it in action. You can find out some more details about using this script by looking at the unit test.
What do you want exactly?, you can get media id out of the text using
$text = 'some stuff [media id=468 width=660 height=440] more stuff';
preg_match("/media id=(.*) w/",$text, $results);
$result = $results[0];
$result = str_replace("media id=","",$result);
$result = str_replace("w","",$result);
$id = $result;

Categories