I am creating my first web data parsing script. I understand that you have to look at the source code and take the info you need from there. But what if the div tags are the same? For example: <div id="1">random number 1</div>...some other code....<div id="1">random number 2</div> I need only random number 2, however both have the <div id ="1">. How can I use the code above but print only the "random number 2"
It would be easier to see the script your wrote, but here is a shot in the dark, hoping that I understand you right: <?php $random = rand(1,10); echo '<div id="1">Random number: ' . $random . '</div>'; ?> PHP:
maybe you can use looping, and only print on the equal of "random number 2". let me know your script, so maybe i can help you to check it
I am guessing the thread was abondoned. If you want this random, a loop is overkill just do as I showed:
your script will only output random number between 0-10. what i mean when using loop, is using it on the inner div, and using it with the pattern that he want to use, maybe like : 0..2..4..6..8 or 0..5..10..15.. that what i mean thanks,
A loop is good if you don't want X showing 5 times before Y finally shows, I give you that for sure. But, I don't hink it matters, PCZ has not replied to anyone yet. lol
Ah, that makes more sense! In this case, I would look at the tags surrounding the div tags in question. Do you have a better more full example?
The tags are the same, so he's going to use preg_match_all and echo out or store match[0][1] as a variable
I know those tags are the same. However, you can more effectively import data for the purpose of storing in SQL by looking at the tags outside of those tags. For example: <div style="float:left;clear:left"> <div class="title">Random Number 1</div> <div id="1">random number 1</div> </div> <div style="float:left;clear:right"> <div class="title">Random Number 2</div> <div id="1">random number 2</div> </div> HTML: In this case I can grab #1 separately while skipping any that he does not want, by including the container tags as well. Better than just grabbing all of the values off a page indiscriminately, in other words more control. But I could be complicating it more than actually needed, I just like to be very precise.
<?php $text = '<div style="float:left;clear:left"> <div class="title">Random Number 1</div> <div id="1">123456</div> </div> <div style="float:left;clear:right"> <div class="title">Random Number 2</div> <div id="1">654321</div> </div>'; preg_match_all('#<div id="1">([0-9]+)</div>#', $text, $match_random); $random1 = $match_random[0][0]; // 123456 $random2 = $match_random[0][1]; // 654321 echo $random2; ?> PHP:
preg_match("#<div id="1">(.*)</div>#", $your_output, $matches); print_r($matches); PHP: with this it will print out your array of matches, if you need the second number you just use $matches[1]