tags:

views:

22

answers:

1

hi, I am using PHP 4, that is what the host has at the moment. How can I extract link from a string when given part of the link to find.

Example

$find_string = 'http://www.mysite.com/apple';  
$string = 'Something and something else 
          <a href="http://www.mysite.com/apple_banana"&gt;testlink&lt;/a&gt; 
          something else and so forth 
          <a href="http://www.mysite.com/orange"&gt;orange&lt;/a&gt;

In this case I would like to extract only the links that has http://www.mysite.com/apple in it so it would retrieve http://www.mysite.com/apple_bananan

Any help would be greatly appreciated.

A: 
$matches = array();
$find_string = 'http://www.mysite.com/apple';
preg_match_all('!<\s*a\s*href="('.$find_string.'[^"]+)">!', 'Something and something else < a href="http://www.mysite.com/apple_banana"&gt;testlink&lt; /a> something else and so forth < a href="http://www.mysite.com/orange"&gt;orange&lt; /a> ', $matches);

print_r($matches);

/* output:

Array
(
    [0] => Array
        (
            [0] => < a href="http://www.mysite.com/apple_banana"&gt;
        )

    [1] => Array
        (
            [0] => http://www.mysite.com/apple_banana
        )

)

*/
webbiedave