Jump to content

[SOLVED] Find all elements that match on a page


Recommended Posts

Hi,

 

I've been using explode to split the contents of a page for a while now, if I wanted to find "<span class="hello">hey there!</span>" on a page I'd just do "explode('<span class="hello">hey there!</span>', $page);" however now I'm in a position where "<span class="hello">hey there!</span>" appears multiple times on a page and I need to retrieve each one.

 

How would I go about doing this for multiple things? I'll give an example of the output I want to check:

 

<div class="something">Hi there!</div>
<div class="something">Hello!</div>
<div class="something">hi i am</div>
<div class="something">rofl</div>
<div class="something">dsfssdf</div>

 

and I want to get the contents of each <div class="something">. I assume this will require more than just using explode, regex maybe? I know how to get a single one using explode, but not multiple.

 

So the final result would be an array like:

 

$somethings[0] = "Hi there!";
$somethings[1] = "Hello!;
$somethings[2] = "hi i am";

 

any ideas?

 

:)

Like this

<?php
$HTML = '<div class="something">Hi there!</div>
<div class="something">Hello!</div>
<div class="something">hi i am</div>
<div class="something">rofl</div>
<div class="something">dsfssdf</div>';
preg_match_all('%<div class="[^"]*">([^<]*)</div>%sm', $HTML, $somethings);
$somethings = $somethings[1];
var_dump($somethings);
?>

 

array(5) {

  [0]=>

  string(9) "Hi there!"

  [1]=>

  string(6) "Hello!"

  [2]=>

  string(7) "hi i am"

  [3]=>

  string(4) "rofl"

  [4]=>

  string(7) "dsfssdf"

}

This works perfectly, however if I want to go "more advanced" I'm sort of lost:

 

If I wanted to find EVERYTHING between <div class="something">{HERE}</div> how would I do it? Let's say, for example, I have:

 

<div class="something"><div id="rofl">dfssdffds</div><img src="lol.jpg"><a href="#">dfsffs</a></div>

 

and I want to match between

 

<div class="something"></div>

 

So I want it to return:

 

<div id="rofl">dfssdffds</div><img src="lol.jpg"><a href="#">dfsffs</a>

 

How would I match that? I tried playing around with it but I'm lost, any ideas?

 

Thanks for the help so far :)

Welcome, RegEx gets easier with time, I'm teaching a guy at work, his slower getting to grips with it, but the first time i showed him.. it said "F*** that!" but when i updates his 20someting lines of broken code with 3 lines of working code.. he started pay attention.

Welcome, RegEx gets easier with time, I'm teaching a guy at work, his slower getting to grips with it, but the first time i showed him.. it said "F*** that!" but when i updates his 20someting lines of broken code with 3 lines of working code.. he started pay attention.

 

heh, yeah, I'm a sort of "hmm, that does look very hard but I guess it'll save me time..." then end up quitting because it's too complicated for my non-programmer brain to handle :)

 

on that note, can't get adding new lines to work now. by default . ignores new lines, but mine has new lines and adding /s (the modifier) doesn't seem to work. Damn you regex!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.