2013-06-22 43 views
0

本質上,我從URL中拉出了文本,並且需要找到一種方法從文本中提取特定字符。從php文件中提取特定數據

線我需要拉是:

<p align="center"><a href="http://sitexplosion.com/?rid=1256" target="_blank">http://sitexplosion.com/?rid=1256</a></p> 

我需要拉文本基本上是數1256,基本上一切後襬脫=與之前的「TARGET =」 _空白「>

那號碼將會改變,長度可能在1到6個字符之間

如果已經發布了這樣的內容,我很抱歉,我一直在網上搜索最近3個小時,試圖找到某種答案。

如果你能告訴我如何從那一行拉出那些角色,我已經休息了。

在此先感謝!

+0

$ _GET [ '擺脫']會爲你做到這一點? –

+0

我想你需要從字符串中提取信息,所以爲了你的需要,我需要preg_match – CroiOS

+0

,要使用preg_match,我需要知道我正在查找的字符,而我不知道。我只知道那些角色前後是什麼。 – user2511559

回答

1

這個怎麼樣: -

$strout="<p align='center'><a href='http://sitexplosion.com/?rid=1256' target='_blank'>http://sitexplosion.com/?rid=1256</a></p>"; 
$startsAt = strpos($strout, "?rid") + strlen("?rid="); 
$endsAt = strpos($strout, "{\'target}", $startsAt); 
$result = substr($strout, $startsAt, ($endsAt-3) - $startsAt); 
echo $result; 

輸出: -

enter image description here

+0

要使用$ _GET,則需要在URL中顯示該信息。我有的代碼只是一個.php文件中的特定行。除非我錯了或丟失了一些東西... – user2511559

+0

@ user2511559看到我更新的答案。 –

1

在這裏,爲什麼不使用HTML解析器或DOM文檔中提取的聯繫,然後得到的鏈接查詢參數with parse_url()

$html = ' 
<p align="center"><a href="http://sitexplosion.com/?rid=1256" target="_blank">http://sitexplosion.com/?rid=1256</a></p> 
<p align="center"><a href="http://sitexplosion.com/?rid=123456" target="_blank">http://sitexplosion.com/?rid=123456</a></p> 
<p align="center"><a href="http://sitexplosion.com/" target="_blank">http://sitexplosion.com/</a></p> 
'; 

libxml_use_internal_errors(true); 
$dom = new DOMDocument(); 
$dom->loadHTML($html); 

$link_ids = array(); 
foreach ($dom->getElementsByTagName('a') as $link) 
{ 
    if($query = parse_url($link->getAttribute('href'), PHP_URL_QUERY)) 
    { 
     $link_ids[] = str_replace('rid=','',$query); 
    } 
} 

print_r($link_ids); 
/* 
Array 
(
    [0] => 1256 
    [1] => 123456 
) 
*/ 

希望它有幫助

0

嗯,這是更短

$string = strip_tags('<p align="center"><a href="http://sitexplosion.com/?rid=1256" target="_blank">http://sitexplosion.com/?rid=1256</a></p>'); 

echo str_replace('http://sitexplosion.com/?rid=','',$string);