Domanda

I'm pulling in a calendar from an external site with file_get_contents, so I can use jQuery .load on it.

In order to fix the relative path issues with this approach, I'm using preg_match_all.

So doing

preg_match_all("/<a href='([^\"]*)'/iU", $string, $match);

Gets me all the occurrences of <a href = '' What I'm after are the just the links inside the single quotes. Now each link starts with "?date" so I have <a href='?date=4%2F9%2F2014&a' etc.

How can I efficiently get the string between single quotes in all <a href= occurrences.

È stato utile?

Soluzione

Use the Dom parser to get the href from the <a> tag

<?php
$file =  "your.html";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$elements = $doc->getElementsByTagName('a');
foreach ($elements as $tag) {
    echo $tag->getAttribute('href');
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top