Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    stripping data out of a string

    hello
    i am new here , i have problem with patterns , and i can't figure out how to get the data out of this

    <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (2)">

    please tell me how

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    And what are you trying to get out of there?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    you can take data out with the backspace key
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #4
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    yes
    i am sorry i forget to mention that
    i want to get the url and everything inside title="HERE"

    <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (2)">

    i have a lot of lines
    not just 1
    and every one has a unique url and a title

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You can use pattern matching with a pattern of '~title=".*"~', but that will apply to anything with a title, not just the urls. It'll take awhile to write that up.
    Is the document well formed? If it is, you can use the DOMDocument to extract it:
    Code:
    $s = '
    <html>
    <body>
     <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (1)">
     <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (2)">
     <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (3)">
     <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (4)">
     <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (5)">
    </body>
    </html>';
    
    $dom = new DOMDocument('1.0', 'utf8');
    $dom->loadHTML($s); // use loadHTMLFile if you are using a file
    $anchors = $dom->getElementsByTagName('a');
    $aAnchors = array();
    foreach ($anchors AS $itm)
    {
        if ($itm->attributes != null)
        {
            if ($title = $itm->attributes->getNamedItem('title'))
            {
                $aAnchors[] = $title->value;
            }
        }
    }
    
    print_r($aAnchors);
    Takes a lot more memory, but sure is easier to do.

    Edit:
    Actually, I just realized that its very forgiving. The 'a' tags are all invalid up there, but it seems to parse it alright.
    Last edited by Fou-Lu; 03-20-2013 at 06:31 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    rahim3000 (03-24-2013)

  • #6
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rahim3000 View Post
    hello
    i am new here , i have problem with patterns , and i can't figure out how to get the data out of this

    <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (2)">

    please tell me how

    yes i forget to tell you that i want to read this data from a file
    each line contain this
    <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (2)">
    i want to get the url + all what's inside title

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    If you use the dom, you can extract both:
    PHP Code:
    <?php

    $s 
    '
    <html>
    <body>
     <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (1)">
     <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (2)">
     <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (3)">
     <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (4)">
     <a href="http://www.website.com/xxxxxx/blabla/" title="TiTle (5)">
     <a href="http://www.website.com/xxxxxx/blabla/">
    </body>
    </html>'
    ;

    $dom = new DOMDocument('1.0''utf8');
    $dom->loadHTML($s); // use loadHTMLFile if you are using a file
    $anchors $dom->getElementsByTagName('a');
    $aLinkInfo = array();
    foreach (
    $anchors AS $itm)
    {
        if (
    $itm->attributes != null)
        {
            
    $aItem = array();
            if (
    $title $itm->attributes->getNamedItem('title'))
            {
                
    $aItem['title'] = $title->value;
            }
            if (
    $url $itm->attributes->getNamedItem('href'))
            {
                
    $aItem['url'] = $url->value;
            }
            if (!empty(
    $aItem))
            {
                
    $aLinkInfo[] = $aItem;
            }
        }
    }

    print_r($aLinkInfo);
    I love the dom.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    rahim3000 (03-24-2013)


  •  

    Posting Permissions

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