SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: REGEX formula.

  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Arrow REGEX formula.

    Hi i know this is very simple for REGEX old timer.

    I have this link below,

    Code:
    <a href="http://domain.org/tlg/1188777994.html">MANAGER NEEDED</a>
    All i want is to get the text "MANAGER NEEDED" from that link.

    I've been formulating for hours now and i'm completely lost i guess.

    Anyone would to show the REGEX for this?

    thanks in advance.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    PHP Code:
    $anchor '<a href="http://domain.org/tlg/1188777994.html">MANAGER NEEDED</a>';
    $text preg_replace('/\<.*?\>(.*?)\<.*?\>/','$1',$anchor);
    echo 
    $text

  3. #3
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks oddz.

    How about i want to get the content of href which is "http://domain.org/tlg/1188777994.html" ?

    Thanks again in advance.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    PHP Code:
    $anchor '<a href="http://domain.org/tlg/1188777994.html">MANAGER NEEDED</a>';
    $info array_combine(array('href','label'),explode('??????',preg_replace('/\<a\shref="(.*?)">(.*?)\<.*?\>/','$1??????$2',$anchor)));

    echo 
    '<p><strong>href:</strong>&nbsp;',$info['href'],'</p>';
    echo 
    '<p><strong>label:</strong>&nbsp;',$info['label'],'</p>';

    echo 
    '<pre>',print_r($info),'</pre>'

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Try taking a look at using a combination of DOMDocument and XPath to get the information you need, IMHO, it's far better suited to parsing HTML than RegExp.
    PHP Code:
    <?php
    $oXPath 
    = new DOMXPath(@DOMDocument::loadHTML(file_get_contents('http://www.google.com')));
    foreach(
    $oXPath->query('//a') as $oLinkNode)
    {
        
    printf(
            
    '%s:- %s<br />%s',
            
    $oLinkNode->nodeValue,
            
    $oLinkNode->getAttribute('href'),
            
    PHP_EOL
        
    );
    }
    /*
        Images:- http://images.google.co.uk/imghp?hl=en&tab=wi<br />
        Video:- http://video.google.co.uk/?hl=en&tab=wv<br />
        Maps:- http://maps.google.co.uk/maps?hl=en&tab=wl<br />
        News:- http://news.google.co.uk/nwshp?hl=en&tab=wn<br />
        Shopping:- http://www.google.co.uk/prdhp?hl=en&tab=wf<br />
        Mail:- http://mail.google.com/mail/?hl=en&tab=wm<br />
        more â–¼:- http://www.google.co.uk/intl/en/options/<br />
        Groups:- http://groups.google.co.uk/grphp?hl=en&tab=wg<br />
        Books:- http://books.google.co.uk/bkshp?hl=en&tab=wp<br />
        Scholar:- http://scholar.google.co.uk/schhp?hl=en&tab=ws<br />
        Finance:- http://www.google.co.uk/finance?hl=en&tab=we<br />
        Blogs:- http://blogsearch.google.co.uk/?hl=en&tab=wb<br />
        Translate:- http://translate.google.co.uk/?hl=en&tab=wT<br />
        YouTube:- http://uk.youtube.com/?hl=en&tab=w1&gl=UK<br />
        Calendar:- http://www.google.com/calendar/render?hl=en&tab=wc<br />
        Photos:- http://picasaweb.google.co.uk/home?hl=en&tab=wq<br />
        Documents:- http://docs.google.com/?hl=en&tab=wo<br />
        Reader:- http://www.google.co.uk/reader/view/?hl=en&tab=wy<br />
        Sites:- http://sites.google.com/?hl=en&tab=w3<br />
        even more Â»:- http://www.google.co.uk/intl/en/options/<br />
        iGoogle:- /url?sa=p&pref=ig&pval=3&q=http://www.google.co.uk/ig%3Fhl%3Den%26source%3Diglk&usg=AFQjCNH9dUJQAsNWnO3XKq2EIPgFbczqlA<br />
        Sign in:- https://www.google.com/accounts/Login?continue=http://www.google.co.uk/&hl=en<br />
        Advanced Search:- /advanced_search?hl=en<br />
        Preferences:- /preferences?hl=en<br />
        Language Tools:- /language_tools?hl=en<br />
        Advertising Programmes:- /intl/en/ads/<br />
        Business Solutions:- /services/<br />
        About Google:- /intl/en/about.html<br />
        Go to Google.com:- http://www.google.com/ncr<br />
        Make Google my homepage:- /aclk?sa=L&ai=Ceac1j40bSuqOCeSCjAe3kNmdDsX4n3yvu5nHCs2tk5cREAEgwVRQvpyhyfj_____AWC7BqoECU_Q4NeHmo6ehw&num=1&sig=AGiWqtzTZ9YJE24JV7xOAsE5m_eKyM5jgw&q=/mgyhp.html<br />
        Privacy:- /intl/en/privacy.html<br />
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •