2010-05-07 108 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 
     ) 

) 

*/