There is a similar question to this on StackOverflow. But, My question is a little different.
I have selected the image with the required class whose image I want. Earlier, I used
element->src
to get the value of src attribute, but now the site has replaced it with 'data-src'.
I do not have the full contents of a tag, hence I can not use preg_replace. I Have the reqired element, I just want to be able to do something like
$element->data-src
I am trying to do this using PHP SIMPLE HTML DOM PARSER, but no luck yet.
Try using
$element->{'data-src'}
Related
I can get all image elements in the DOM like this:
$element->getElementsByTagName('img')
How can I get all image and iframe elements in the DOM in one request? Something like:
$element->getElementsByTagName('img, iframe')
Using getElementsByTagName you cannot get multiple elements by tag name. You can get one tag at a time only. To achieve this, there are two ways
Calling Tag for two times
$element->getElementsByTagName('img')
$element->getElementsByTagName('iframe')
OR
Using JavaScript
document.querySelectorAll('img,ifame')
PS: I would not recommend using PHP for DOM parsing because that will impact page performance.
Hope this helps
I have a html table, generated by another website that I'm trying to convert to a php array.
I can not convert it using simplexml because the code of the generated table is not valid, and cause a lot of errors, also I need to keep some attributes of the table td elements, and remove the others.
What would be the most efficient way of doing this? Or do you know any php class that could help me achieve this?
BTW: What I'm trying to do is convert an school schedule to a php array, that I will be able to exploit after.
Here is an example of the data I retrieve: http://paste2.org/p/1869193
Btw, using php strip tags, I already remove the unnecessary tags such as spans and fonts.
You can also use PHP's Tidy if installed (it is by default on some installs) - it not only cleans up the HTML, but also lets you traverse the DOM:
http://www.php.net/manual/en/book.tidy.php
You can find a list of HTML parserd in the answers of the following question on SO:
Robust and Mature HTML Parser for PHP
I want to get the url of an image (where it's stored) from the description of a RSS feed, so then I can put that url inside a variable.
I know that for getting the link of the RSS feed post I have to use $feed->channel->item->link; where $feed is $feed=simplexml_load_file("link_of_the_feed";.
But what if I have get the image url of the post, do I have to use something like $feed->channel->item->image;?
I really don't know, maybe a RSS parser like MagPie RSS which I tried without results?
Thanks in advance.
If the image node is at the top level of the item node, then yes. If it's deeper than the item node, you'll have to traverse it accordingly. It would be helpful if you posted your XML.
EDIT: you can also check out my answer here on how to parse through an XML file with PHP.
You're on the right track! But it all depends on the format that the RSS Feed is set up in.
The item node actually contains a whole bunch of different fields, of which link is only one. Take a look here for information on the other fields that the item node contains.
Now, if the RSS feed points directly to the image file, then you can just use item->link. More likely, however, the link points to a blog post or something that has the image embedded in it. In this case, you can undertake some processing on $feed->channel->item->description to find what you need. The description node contains an escaped HTML summary of the post, and then from there, you can just use a regular expression to find the source of the image. Also remember: before you start using the regular expressions, you might need to decode the description using htmlspecialchars_decode() before you start processing it with the regular expressions - in my own experience, descriptions often come formatted with special characters escaped.
I know that's a lot of information, but once you get started it's really not as hard as it sounds. Good luck!
This question already has answers here:
Rendering HTML inside textarea
(7 answers)
Closed 7 years ago.
I have code that lets you format the text in a textarea with bold, underline, strikeout or italics. But it displays the html tags instead of actually formatting it. I know once I output it inside a div, it ill be formatted, but I want to show the formatting while the user writes the text.
How can I enable formatting? JavaScript is fine, I'm using JQuery at the moment. I would also rather not use a WYSIWYG editor, if there is another option.
Thanks for any help.
Use contentEditable attribute on div element
<div contentEditable="true">
</div
This way you can format your input in any desired way, just dont forget properly escape data.
Just a thought; if you just want to let users see what their response will look like, you could take a cue from this site and have a sort of "preview" div right above/below the comment box. You could use onKeyUp to put the code into the div to keep it up do date as the user types. It's not perfect, but it's probably as good as you're going to get if you don't want to use a WYSIWYG.
That being said, the contentEditable + WYSIWYG option is probably more user friendly, and simpler, IMHO.
I believe it's only possible by using some div or other styleable html element instead of your textarea. With Javascript you would then add the functionality of the textarea.
How about you use some formatting system like this here on SO? It is much more elegant in my opinion, than pseudo WYSIWYG.
Replace the textarea with an div and enable the designmode.
Or you can use the jquery plugin http://plugins.jquery.com/project/designMode
I want to know how to find a number on a remote website and make it a variable.
For example, if I want to find the stock quote for "AMZN", I would use curl or get contents on the page "http://stock-quotes.com/AMZN" to make it a variable string called $contents
Now that I have $contents, how would I find that AMZN quote? I was thinking of using a regular expression to narrow down the line, like finding "AMZN=35 points", and then perform another function to delete the "AMZN=" and " points" at the start and end of the string so that "35" is all that's left.
Is that how people do it?
1.) DOM Element
2.) Simple XML
3.) preg_match
4.) strpos
What I've always done (say in spidering, etc.) is to use the simple_html_dom library in PHP, then inspect the markup for the site.
The downside, as mentioned before, is that if the markup changes, you'll need to modify your code, but usually it's fairly easy, and if you use a source that has informative markup (consistent class names on the elements you need, etc.), then it's even easier.
Library link: http://simplehtmldom.sourceforge.net/