php - Get certain element of remote site with lack of unique criteria

365

Sorry if this seem unclear will explain it

I am trying to get certain element that may be either<iframe><script> from remote page,

this remote page have many<iframe> and<script> but the one i need have no unique criteria like specificid the most unique criteria is the position of it in the code, its always above this piece of code

<script src="http://code.jquery.com/jquery-1.5.2.min.js" type="text/javascript"></script> 

in other words theiframe orscript i want is just in the line above the jquery reference

what are the methods i can relies on to get this iframe

220

Answer

Solution:

$file = file('www.yoururl.html');

foreach($file as $key => $value) {
    if(strpos($value, 'http://code.jquery.com/jquery-1.5.2.min.js')) {
        echo $file[$key-1];
        break;
    };
};

you could use regex to make sure you didn't pick up anything else on the line:-

    preg_match('/<(iframe|script).+<\/(iframe|script)>/', $file[$key-1], $matches);
    echo $matches[0];

People are also looking for solutions to the problem: php - Find intersecting rows between two 2d arrays comparing differently keyed columns

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.