Eduardo Iglesias
Eduardo Iglesias

Reputation: 1066

preg_match from this code

Hi if someone could help me with my preg_match I want the title from this code

    <dt class="gallery-icon">
    <a href="?attachment_id=31" title="title">
        <img width="150" height="150" src="librosdefirmas-bodas-1-150x150.jpg" class="attachment-thumbnail" alt="Caption">
    </a>
</dt>

i have this:

preg_match_all('/<dt class="gallery-icon">\s*<a href="(.*)" title="(.*)".*>/is', $page_columns[0], $titles);

But it doesn't work who can help me?

Upvotes: 0

Views: 103

Answers (2)

Glavić
Glavić

Reputation: 43552

Don't parse HTML with regular expressions. Read.

$html = '
 <dt class="gallery-icon">
    <a href="?attachment_id=31" title="title">
        <img width="150" height="150" src="librosdefirmas-bodas-1-150x150.jpg" class="attachment-thumbnail" alt="Caption">
    </a>
</dt>
';
$dom_document = new DOMDocument();
$dom_document->loadHTML($html);
$dom_xpath = new DOMXpath($dom_document);
$elements = $dom_xpath->query("//dt/a");
print_r( $elements->item(0)->getAttribute('title') );

Upvotes: 3

asmecher
asmecher

Reputation: 1055

The ".*" is a greedy match, i.e. it'll wander outside of the first quote if there is a later one that it can match. Use [^"]* instead of .* to match all characters but a quote.

Upvotes: 1

Related Questions