2010-05-07 34 views
1

我正在使用PHP 4,这是主机目前的功能。当给定链接的部分查找时,如何从字符串中提取链接。如何在给定部分链接值时提取链接

$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> 

在这种情况下,我想仅提取它具有http://www.mysite.com/apple的联系,因此将检索http://www.mysite.com/apple_bananan

任何帮助将不胜感激。

+0

这个例子有点令人困惑 - 你能清楚你到底想要做什么吗?你想查找'$ string'中是否有'http:// www.mysite.com/apple'的实例? – ABach 2010-05-07 21:47:53

回答

0
$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 
     ) 

) 

*/