Want to extract html link from string

Discussion in 'PHP' started by KRISHNA KUMAR, Jul 21, 2009.

  1. #1
    Hi guy's!,
    can any one help me, how to extract all html link from a string. i have tried alot at best of my level but didn't get optimum result.

    regard's
     
    KRISHNA KUMAR, Jul 21, 2009 IP
  2. mioot

    mioot Peon

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use these below code and try.
     
    mioot, Jul 21, 2009 IP
  3. KRISHNA KUMAR

    KRISHNA KUMAR Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi dear mioot,
    can you explain it. because after implementing it no result displaying.
     
    KRISHNA KUMAR, Jul 21, 2009 IP
  4. Submerged

    Submerged Active Member

    Messages:
    132
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    Something you could try, it's a quick and dirty way to get it done:
    <?
    $linkcount = 0;
    $explode = explode("http://",$string);
    
    for ($round=1;$round< count($explode);$round++)
    {
    $temp = explode(" ",$explode[$round]);
    
    $links[$linkcount] = $temp[0];
    $linkcount++;
    }
    
    print_r($links); // prints a list of the links
    Code (markup):
    That should work fine. I haven't tested it, so give it a run and let me know if it isn't working.
     
    Submerged, Jul 21, 2009 IP
  5. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #5
    Use preg_match function.
    Here is the regex pattern that you can use: $linkPattern = '/<a[\s]+[^>]*?href[\s]?=[\s\"']+(.*?)[\"']+.*?>([^<]+|.*?)?<\/a>/is';

    Amit
     
    samyak, Jul 21, 2009 IP
  6. KRISHNA KUMAR

    KRISHNA KUMAR Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    but dear friend Grunt when any user didn't provide http:// in that case this logic will not as i think.
     
    KRISHNA KUMAR, Jul 23, 2009 IP
  7. Submerged

    Submerged Active Member

    Messages:
    132
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #7
    True (Krishna) . . . samyak probably had a better code, it would grab based on <a href> links and not http://. Course, his wouldn't get any plain text URLs (those not actually put in a hyperlink), so it may depend on what content you are looking at. If all the links on the pages you use the code on will actually be hyperlinked (and not just have plain-text URLs) then samyak's code is the way to go.
     
    Submerged, Jul 23, 2009 IP
  8. KRISHNA KUMAR

    KRISHNA KUMAR Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi i am decode the string in html entities now string look like this.

    <p>I would like to introduce myself, my name is xyz and I am the<br />Director of Sales for the abc Marketing Group.&nbsp; One of my marketing<br />colleagues had informed me that you are interested in learning more about<br />our promotions, rewards and vacation incentive products.<a href=\"http://www.xyz.com\">http://www.xyz.com</a><br /><br />&nbsp;<br /><br />abc remains the #1 finance incentive company in North America, offering<br />more variety and more product options.&nbsp; Odenza is unique in that we are a<br />full service travel incentive group with all the key services to ensure your<br />travel incentive needs are met. This includes our own marketing and graphic<br />design department at your service and most importantly, in-house travel<br />agents to fulfill travel requests from your customers receiving our travel<br />incentives.<a href=\"http://www.xyz.com\">http://www.xyz.com</a><br /><br />&nbsp;<br /><br />We also provide small business products through our Fun Planet line of<br />incentives that can be purchased for as little as $10.00.&nbsp; You can buy as<br />little as 30 certificates to get started.&nbsp; These programs are ideal for thesmall<br />business looking to get an edge on their competition, and generate more<br />sales.<br /><br />Best regards,<br />&nbsp;<br />abc<br />Director of Sales<br /><br />abc financeGROUP INC.<br />When Relationships Really Matter<br /><a href=\"http://www.xyz.com\">http://www.xyz.com</a></p>

    i am store this htmlentity in variable and using preg_match_all and pass three parameter into the preg_match_all function. after that when i am print array then it didn,t dispaly anything.

    Here is the out put


    Array
    (
    [0] => Array
    (
    )

    [1] => Array
    (
    )

    [2] => Array
    (
    )

    )

    please kindly help me
     
    KRISHNA KUMAR, Jul 23, 2009 IP