2013-08-21 16 views
0

在某一段文字,我們有幾個鏈接,發現:preg_replace函數/的foreach改變網址的文字

$regex = '\b(http://www.domain.com/)[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]'; 

我們想改變這些URL到新的,如:

http://www.domain.com/news/item.php?ID=12321&TYPE=25成:/ news/page/$ arttype- $ artID/

$ url列出了其中幾個url的列表,但我們似乎無法在$ message中更新它們。

這是到目前爲止的代碼:

$string = "$message"; 

function do_reg($text, $regex) { 
    preg_match_all($regex, $text, $result, PREG_PATTERN_ORDER); 
    return $result[0]; 
} 

$A =do_reg($string, $regex); 
foreach($A as $url) { 
    $check = parse_url($url, PHP_URL_QUERY); 

    preg_match("/ID=([^&]+)/i", $check, $matches); 
    $artID = $matches[1]; 

    preg_match("/TYPE=([^&]+)/i", $check, $matches); 
    $arttype = $matches[1]; 

    preg_replace("$url", "/news/page/$arttype-$artID/", $text); 
} 

有誰知道如何更新在$消息中找到的所有唯一的URL的?

-------使用V-Tech的代碼----------------

$message = " 
<li><strong><a href="http://www.domain.com/news/item.php?ID=12321&TYPE=25" target="_blank">Link 1</a></li> 
<li><strong></strong><a href="http://www.domain.com/news/item.php?ID=12300&TYPE=2" target="_blank">Link 2</a></li> 
<li><a href="http://www.domain.com/news/item.php?ID=12304&TYPE=2" target="_blank">Link 3</a></li> 
<li><a href="http://www.domain.com/news/item.php?ID=12314&TYPE=2" target="_blank">Link 4</a></li>"; 

$pattern = "/(http:\/\/www\.domain\.com)\/news\/item\.php\?ID=([^&]+)&TYPE=(\d+)/g"; 
$replacement = "\${1}/news/page/\${2}-\${3}/"; 
preg_replace($pattern, $replacement, $message); 
echo "$message "; 
+1

哦,世界發生了什麼事嗎?你知道這個腳本非常慢,因爲你已經在循環中放了幾個'preg_'。我會指出:** 1 - **表示您的正則表達式缺少分隔符,這意味着它是無效的。所以請啓用錯誤報告,這可以節省您的頭痛。 ** 2 - **您可以使用'preg_replace_callback()'一次完成所有這一切。 – HamZa

+0

對不起。這是第一次與這個有幾場比賽等工作仍然沒有線索如何解決它,但:-) – KJS

+0

@KJS正如'HamZa'指出,你缺乏分隔符。例如:'preg_match(「/ \ bweb \ b/i」'---在preg_math()'=> http://上查閱(取自)PHP手冊。 //php.net/manual/en/function.preg-match.php –

回答

1

大約在一個命令做什麼呢?

$pattern = "/(http:\/\/www\.domain\.com)\/news\/item\.php\?ID=([^&]+)&TYPE=(\d+)/"; 
$replacement = "\${1}/news/page/\${2}-\${3}/"; 
$result = preg_replace($pattern, $replacement, $message); 

基本上,它削減的信息,三塊(方案://域名,ID值和類型值),並通過這三件插入$替換字符串廚師新的URL。

其中ID=([^&]+)&TYPE=(\d+)假設ID值可以是任何東西(小心html實體以&開頭)直到&。這裏的TYPE值被假定爲數值。因此請將其改爲您的需求。

更新:從$模式去除g標誌符,preg_replace()結果放在$結果

+0

感謝V-tech,但不知何故它仍然無法正常工作。我已經在上面添加了代碼和解決方案。也許你可以看到發生了什麼問題。 (當然在php中是「\」) – KJS

+0

@KJS更新爲您的需求。 HamZa的eval.in幫了很多。 –

+3

使用除/以外的模式分隔符,所以你不必逃避所有這些文字/的。 #或〜會運作良好:'$ pattern =「#(http:// www \ .domain \ .com)/news/item\.php\?ID=([ ^&] +)&TYPE =(\ d +) #「' –

1

From : http://www.domain.com/news/item.php?ID=12321&TYPE=25
To : http://www.domain.com/news/page/25-12321/

$string_start = '<li><strong><a href="http://www.domain.com/news/item.php?ID=12321&TYPE=25" target="_blank">Link 1</a></li> 
<li><strong></strong><a href="http://www.domain.com/news/item.php?ID=12300&TYPE=2" target="_blank">Link 2</a></li> 
<li><a href="http://www.domain.com/news/item.php?ID=12304&TYPE=2" target="_blank">Link 3</a></li> 
<li><a href="http://www.domain.com/news/item.php?ID=12314&TYPE=2" target="_blank">Link 4</a></li>'; 

$string_end = $string_start; 
$string_end = preg_replace("/(https|http):\/\/(w{0,3}\.{0,1})(domain\.com)\/news\/item\.php\?ID=([0-9]*)(&amp;|&)TYPE=([0-9]*)/", "$1://$2$3/news/page/$6-$4/", $string_end); 
+0

也謝謝你D3F4ULT。今天我學到了很多東西,謝謝大家。 – KJS