Jump to content

[SOLVED] Strip all content before and after certain strings, Regex maybe?


Dale_G

Recommended Posts

Okay, there is webpage which I'm getting through file_get_contents.

 

The part I want to get is this

 

<td align="right" class="head-row">
<div id="numbertext">745</div>
<br><br>
<div id="numbertext">873</div>
<br><br>
<div id="numbertext">2324</div>
<br><br>
</td>

 

But, no all of that, you see the number 745? I'd need to be able to get that number by itself. Don't really need the other numbers, just the first number between the <div id="numbertext"> and the </div> tags. The thing is...the div '<div id="numbertext">' appears multiple times before AND after this block of code, but the '<td align="right" class="head-row">' only appears once, which is why I BELIEVE it needs to be..focused on.

 

Not sure though, basically..you see from the above code, I need to get that number 745 by itself. Thanks guys! ;D

Link to comment
Share on other sites

<?php

$html = <<<HTML
<td align="right" class="head-row">
<div id="numbertext">745</div>
<br><br>
<div id="numbertext">873</div>
<br><br>
<div id="numbertext">2324</div>
<br><br>
</td>
HTML;
$replace = "|\\2|";
$new = eregi_replace("<div ([a-zA-Z0-9 \"=]+)>([0-9]+)</div>", $replace, $html);
$new = explode("|", $new);
foreach ($new as $v) {
  	if (is_numeric($v)) {
  		$numbers[] = $v;
  	}
}
print_r($numbers);
?>

 

Worked for me.

Link to comment
Share on other sites

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.