Using simplehtmldom in PHP how do I get the data-href attribute? - php

I am using this PHP library to work with a dom :http://simplehtmldom.sourceforge.net/
I am wanting to access the data-href element of a li element on this page:http://www.spareroom.co.uk/flatshare/bristol/
according to the api reference:
http://simplehtmldom.sourceforge.net/manual_api.htm
This code should work - so long as $res represents the li dom node - which in my case it does:
echo $res->data-href;
However when i run that the echo is "0".... when I would expect to see something like :
"/flatshare/fad_click.pl?fad_id=3248085&search_id=&offset=0&city_id=&flatshare_type=offered&search_results=%2Fflatshare%2Fbristol%2F&"
Can somebody please help me to understand what I am doing wrong

$res->data-href is parse as
$res->data - href
i.e. it's a subtraction, because - is not a valid character in an identifier. Try:
$res->{"data-href"}

Since you are accessing a object keys with special characters have to be quoted and surrounded with {}.
So:
echo $res->data-herf
Should be:
echo $res->{"data-href"}
In all honesty though it probably just easier(and safer) to use the method:
$res->getAttribute("data-href");

Related

PHP Add double quotes before and after variable in json string

I'm trying to add double quotes to Every Variable like file: and label: in a json string. For example:
{file:"File_Name.mp3"},{file:"File_Name.mp4",label:"720"},{file:"File_Name.mp4",label:"360"}
Should be:
{"file":"File_Name.mp3"},{"file":"File_Name.mp4","label":"720"},{"file":"File_Name.mp4","label":"360"}
How can i do this? I read a article of stackoverflow There but my problem not solved with this. I'm supposed to use regular expressions. Unfortunately, but i am new.
and i need to get file name if label is 360 so what will be the php code give the filename as i point-out in php.
Thanks.
OK, if you're new to all this, maybe regular expressions aren't the best answer.
You can also use a simple str_replace function (doc) like this :
str_replace(array('file:', 'label:'),array('"file":', '"label":'),$your_json_string);
To exploit the json in php, you'll need to decode it using json_decode, and use a loop to check the label values (I advise a foreach loop).

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).

Accessing XML attributes data

I have two lines of XML data that are attributes but also contain data inside then and they are repeating fields. They are being stored in a SimpleXML variable.
<inputField Type="Name">John Doe</inputField>
<inputField Type="DateOfHire">Tomorrow</inputField>
(Clearly this isnt real data but the syntax is actually in my data and I'm just using string data in them)
Everything that I've seen says to access the data like this, ,which I have tried and it worked perfectly. But my data is dynamic so the data isn't always going to be in the same place, so it doesn't fit my needs.
$xmlFile->inputField[0];
$xmlFile->inputField[1];
This works fine until one of the lines is missing, and I can have anywhere from 0 to 5 lines. So what I was wondering was is there any way that I can access the data by attribute name? So potentially like this.
$xmlFile->inputField['Name'];
or
$xmlFile->inputField->Name;
I use these as examples strictly to illustrate what I'm trying to do, I am aware that neither of the above lines of code are syntactically correct.
Just a note this information is being generated externally so I cannot change the format.
If anyone needs clarification feel free to let me know and would be happy to elaborate.
Maybe like this?
echo $xmlFile->inputField->attributest()->Name;
And what you're using? DOMDocument or simplexml?
You don't say, but I assume you're using SimpleXMLElement?
If you want to access every item, just iterate:
foreach ($xmlFile->inputField as $inputField) { ... }
If you want to access an attribute use array notation:
$inputField['Type']
If you want to access only one specific element, use xpath:
$xmlFile->xpath('inputField[#Type="Name"]');
Perhaps you should read through the basic examples of usage in the SimpleXMLElement documentation?
For example you can a grab a data:
$xmlFile = simplexml_load_file($file);
foreach($xmlFile->inputField as $res) {
echo $res["Name"];
}

missing ) after argument list

I'll show the main parts of the code as most of it is irrelevant:
$url = $row['url'];
echo "<div id='anything'><img id='$url' src='$cover' alt='$title' onclick='myFunction($url)'>";
and the javascript function:
function myFunction(something) {
alert (something);
}
I recieve the following error in firebug:
missing ) after argument list
[Break On This Error]
myFunction(http://anything.com/anything...
-------------------^
I am relatively new to javascript but I can tell it is obviously not allowing the ":" from the url. However, I can't change or alter the id, as I need to alert the exact id of the Image.
I have made this work in a different format without the php, so I assume it's there where the problem lies?
The URL needs to be a string, but you're just outputting the string's contents.
You could just put quotes around it as suggested elsewhere, but that's at best an incomplete solution.
Fortunately, PHP gives you a better answer: json_encode combined (in your case) with htmlspecialchars. This is a function that (amongst other things) will properly wrap a string for you such that you can use it in JavaScript code. So:
$escapedUrl = htmlspecialchars(json_encode($url));
then
...onclick='myFunction($escapedUrl)'...
json_encode is for encoding text as JSON, but as JSON is a subset of JavaScript literal notation, and json_encode quite happily returns a valid, properly-escaped JavaScript string...
You need the htmlspecialchars as well because you're then outputting the JavaScript code you're generating into the onclick attribute, and the content of all HTML attributes in HTML text (even ones with code in them) must be properly encoded so (for instance) & must be &, etc.
Do this:
$escapedString = json_encode($url);
echo "<div id='anything'><img id='$url' src='$cover' alt='$title' onclick='myFunction($escapedString)'>";
T.J. Crowder is right, and you can check this out for more information:
What is the correct way to escape text in JSON responses?
Why is my first solution incorrect (even if it seems to work with 1 case)?
Read this: http://kunststube.net/escapism/
echo "<div id='anything'><img id='$url' src='$cover' alt='$title' onclick='myFunction(\"$url\")'>";
You basically need to print double quotes around the the value passed into the myFunction call:
onclick='myFunction(\"$url\")'
This is because you are doing something like this:
myFunction(http://anything.com)
Function parameter need to be enclosed within quotes or doble quotes in case of string parameters:
myFunction("http://anything.com")
So your echo should look like:
"<div id='anything'><img id='$url' src='$cover' alt='$title' onclick='myFunction(\"$url\")'>"
Also you should take into account that $url doesn't have to contain valid characters, so you should add some encoding/escaping (think in terms of XSS).
You have to use the encodeURI(uri) function:
"<div id='anything'><img id='$url' src='$cover' alt='$title' onclick='myFunction(encodeURI(\'$url\'))'>";

e-mail as object name

I have a XML with a node called 'e-mail'. I use simplexml_load_file to read the file but when i want to get the row value with $row->e-mail i get just get 0 back.
What's wrong here, all other names work fine so i think it has something to do with 'mail'.
tnx
From the manual
Accessing elements within an XML
document that contain characters not
permitted under PHP's naming
convention (e.g. the hyphen) can be
accomplished by encapsulating the
element name within braces and the
apostrophe.
echo
$xml->movie->{'great-lines'}->line;
So you need something like
$row->{'e-mail'}
This should work:
$row->{'e-mail'}

Categories