0

Inside my HTML I have this <link rel="image_src" href="data/20/1.jpg" />

I need to retrieve the href (data/20/1.jpg).

I tried the code below without any luck

function fetch_rel($url)
{
    $data = file_get_contents($url);
    $dom = new DomDocument;
    @$dom->loadHTML($data);

    $xpath = new DOMXPath($dom);
    # query metatags with og prefix
    $metas = $xpath->query('//*/link[starts-with(@rel, \'image_src\')]');

    $og = array();

    foreach($metas as $meta){
        # get property name without og: prefix
        $property = str_replace('image_src', '', $meta->getAttribute('rel'));
        # get content
        $content = $meta->getAttribute('href');
        $og[$property] = $content;
    }

    return $og;
}
$og = fetch_rel($u);
$image_src = $og['image_src'];

Any suggestions?

1
  • 1
    Could you please show an example of input and how output should look like? Commented Dec 4, 2015 at 12:14

1 Answer 1

1

I think you've made just a little error:

You try to access $og['image_src'];, but your array has no index image_src, because you replaced image_src with ''.

You have to replace this line:

# get property name without og: prefix <-- By the way: you have no og: prefix ;)
$property = str_replace('image_src', '', $meta->getAttribute('rel'));

with this:

$property = $meta->getAttribute('rel');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.