Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

html parser simple dom 1

Status
Not open for further replies.

someoneneedshelps

Technical User
Mar 3, 2016
75
GB
Does anyone know anything about simple dom parser? I'm look at a problem with the find function[pre]foreach($html->find('span itemprop="name"') as $cat)
echo $cat->src;[/pre]

even though there is only one instance I get more after it>? how do I get first instance
 
Not sure what you are attempting to return with itemprop="name".

If you want to specify a span with a property named "itemprop" that has a value of "name", you can do something like:

foreach($html->find('span[itemprop=name]') as $cat)

if you are looking for a span that has a property named "name" with any value, then:

foreach($html->find('span[name]') as $cat)


beyond that, spans don't have src attributes so doing $cat->src will return nothing. Using the "plaintext" option instead will return the text inside the span.

Code:
foreach($html->find('span[name]') as $cat)
echo $cat->plaintext;

This might also help:

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
thanks will try it, the text i want is inside
Code:
<span itemprop="name">

... can i also ask, why does'nt simple dom let you put stuff into an array?
Code:
foreach($html->find('img')as $img)
   $items[]=$img->src;

print_r ($items);
 
this one worked for reference to this thread,
Code:
foreach($html->find('span[itemprop=name]') as $cat)

I cant understand why I have to dismantle the html page source into something different
 
I'm getting there BUT... if I have a number of th elements condition as a title is one of them, what is the find() on that please? inside the html elements is this src or name?

4-Condition:
 
You mean your html is something like: <th title="somethinghere">?

If so just use the same general structure:

Code:
$html->find(th[title]);

This looks for th tags that have a title attribute or property.

Only image tags and iframes have a "src" attribute. ( <img src="path/to/img">, <iframe src="path/to/file.html"> ) So only if you are trying to get the image's or iframes path would you use "src". If you are trying to get the contents of any other HTML element, use "plaintext".

... can i also ask, why does'nt simple dom let you put stuff into an array?

It does.

For non image elements this works for me:
$myarray[] = $element->plaintext;

I cant understand why I have to dismantle the html page source into something different

Not sure what you mean. If you are looking for a specific element then you need to tell the find() function what it is. You are not dismantling anything, you are specifying what you are looking for in a way that makes sense to the dom parser.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top