开发者

How to extract link when given partial link value

开发者 https://www.devze.com 2022-12-29 07:36 出处:网络
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.

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">testlink</a> 
          something else and so forth 
          <a href="http://www.mysite.com/orange">orange</a>

In this case I would like to extract only the links 开发者_Go百科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.


$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">testlink< /a> something else and so forth < a href="http://www.mysite.com/orange">orange< /a> ', $matches);

print_r($matches);

/* output:

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

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

)

*/
0

精彩评论

暂无评论...
验证码 换一张
取 消