2011-12-22 98 views
0

我有一個字符串,它是由第三方網絡服務自動生成的,我想從字符串中獲取網址。該字符串看起來是這樣的:解析字符串以獲取網址

'document.write("<div class=\"display_archive\"><div class=\"campaign\">20\/12\/2011 - <a href=\"http:\/\/us2.campaign-archive1.com\/?u=fdf89fgd7sdf7d8&id=ffd89dfef3\" title=\"News\" target=\"_blank\">News<\/a><\/div><\/div>");' 

我要檢索的URL,在上述情況下,這個網址:http://us2.campaign-archive1.com/?u=fdf89fgd7sdf7d8 & ID = ffd89dfef3 \,我想刪除逃逸反斜槓,所以網址是:http://us2.campaign-archive1.com/?u=fdf89fgd7sdf7d8&id=ffd89dfef3/

我已經嘗試了一些不同的解析器和正則表達式,但我沒有在正則表達式中那麼強大,並且似乎無法正確獲取URL。我試過這個preg_match,但它不起作用,只返回空陣列:

%^((http?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i 

任何幫助,非常感謝。

真誠
- Mestika

回答

1

試試這個:

<?php 
$response = 'document.write("<div class=\"display_archive\"><div class=\"campaign\">20\/12\/2011 - <a href=\"http:\/\/us2.campaign-archive1.com\/?u=fdf89fgd7sdf7d8&id=ffd89dfef3\" title=\"News\" target=\"_blank\">News<\/a><\/div><\/div>");'; 

preg_match('/href=\\\\\"([^\"]+)/', $response, $matches); 

echo 'Raw URL: ' . $matches[1] . '<br />'; 
echo 'Clean URL: ' . stripslashes($matches[1]); 
?> 
0

你的正則表達式是行不通的斜線因。分析只需通過stripslashes()的字符串,然後申請正則表達式

0

你試過str_replace()。 e.g)

$url = "http:\/\/us2.campaign-archive1.com\/?u=fdf89fgd7sdf7d8&id=ffd89dfef3"; 
$url = str_replace('\\', '', $url);