regular expression

Discussion in 'PHP' started by ssimon171078, Nov 3, 2014.

  1. #1
    ssimon171078, Nov 3, 2014 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #2
    You could find links with
    <?php 
    $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>"; 
    PHP:
    However I would recommend using xpath for parsing as it is much easier
    http://php.net/manual/en/class.domxpath.php
     
    Anveto, Nov 3, 2014 IP
  3. ROOFIS

    ROOFIS Well-Known Member

    Messages:
    1,234
    Likes Received:
    30
    Best Answers:
    5
    Trophy Points:
    120
    #3
    As an alternative to regex and dom there's also php's string position functions you could consider to. Try this for example;

    <?php
    $subject = '10px;">
             <div class="post-title" style="margin: -5px 1px 2px 0px;"><h2><a href="https://www.tradebit.com/filedetail.php/276221757-local-marketing-goldrush">
               Local Marketing Goldrush</a></h2></div>
               <div style="line-height: 1.4em; font-size: 1.2em;">';
    
    $start = strpos($subject,'a href="');         
    $end = strpos($subject,'">',$start);
    $output = substr(substr($subject, $start, $end - $start), 8);
    echo $output;
    ?>
    PHP:

    You can apply simple math to extract the url this way, for multiple returns
    though you'd need to bundle code into a function and call via an array.

    .
     
    ROOFIS, Nov 14, 2014 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,875
    Likes Received:
    4,547
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Another alternative is to use DOMDocument which has a javascript feel about it. Where I was searching for 'img' you'd search for 'a'
    
    $document = new DOMDocument();
       libxml_use_internal_errors(true);
        $document->loadHTML($html_source);
        libxml_clear_errors();
    $images = array();
    
    foreach($document->getElementsByTagName('img') as $img) {
        $src = $img->getAttribute('src');
        if (!empty($src)) {
            $images[$src] = "<meta property='og:image' content='{$src}' />";
        }
    }
    PHP:
     
    sarahk, Nov 14, 2014 IP