2014-01-05 96 views
0

我寫了2個函數。首先是這樣的:
preg_replace只作用於第一場比賽

function Find($string, $first, $last) 
{ 
    $string = " ".$string; 
    $action = strpos($string,$first); 
    if ($action == 0) return ""; 
    $action += strlen($first);  
    $result = strpos($string,$last,$action) - $action; 
    return substr($string,$action,$result); 
} 

而第二個位置:

function Replace($string) 
{ 
    $find = Find($string, '#*','*#'); 
    return preg_replace('/'.$find.'/', '___', $string); 
} 

我想取代它與#*開始,以*#結束的字符串。這裏是我的示例文本:Donec gravida #*c_01*# quam nec pulvinar #*c_02*# facilisis. Donec sed consectetur #*c_03*# lectus.

當我使用這樣的功能:echo Replace($string);只有第一個字符串替換,其他不是。像這樣:Donec gravida #*___*# quam nec pulvinar #*c_02*# facilisis. Donec sed consectetur #*c_03*# lectus.

你能幫我嗎?我如何管理這個?

+0

http://us2.php.net/manual/en/function.preg-replace.php – bucabay

+0

是的,我閱讀文檔開始我試圖用限制器像@ Session的用法。但結果是一樣的:沮喪... –

+0

爲你的正則表達式添加一個全局標誌? '/ foo/g' – rlemon

回答

0

嘗試設置極限參數preg_replace

preg_replace('/'.$find.'/', '___', $string, 1); 
+0

首先我嘗試了像@ Session的用法這樣的限制器。但結果是一樣的:沮喪... –

0

我想你想提取一對標誌(#**#)之間的值,然後將它們格式化成另一種形式:

例如,轉換:

Donec gravida #*c_01*# quam nec pulvinar #*c_02*# facilisis. 
Donec sed consectetur #*c_03*# lectus 

Donec gravida link=?c_01 quam nec pulvinar link=?c_02 facilisis. 
Donec sed consectetur link=?c_03 lectus 

如果是的話,你可以在一個普通的前使用組捕獲PRESSION:

/(?:#\*)+([^#\*]+)+(?:\*#)/ 

正則表達式將提取#**#之間的值。

<?php 
$str = 'Donec gravida #*c_01*# quam nec pulvinar #*c_02*# facilisis. Donec sed consectetur #*c_03*# lectus'; 
$matches = array(); 
$ptn = '/(?:#\*)+([^#\*]+)+(?:\*#)/'; 
preg_match_all($ptn, $str, $matches); 
var_export($matches); 

的輸出將是:

array (
    0 => 
    array (
    0 => '#*c_01*#', 
    1 => '#*c_02*#', 
    2 => '#*c_03*#', 
), 
    1 => 
    array (
    0 => 'c_01', 
    1 => 'c_02', 
    2 => 'c_03', 
), 
) 

$matches[0]將包含匹配的完整圖案的文本,$matches[1]將具有相匹配的第一捕獲括號中的子模式的文本

所以,只需使用preg_replace()即可進行轉換。

<?php 
$str = 'Donec gravida #*c_01*# quam nec pulvinar #*c_02*# facilisis. Donec sed consectetur #*c_03*# lectus'; 
$matches = array(); 
$ptn = '/(?:#\*)+([^#\*]+)+(?:\*#)/'; 
$str = preg_replace($ptn, 'link?=$1', $str); 
echo $str, "\n" 

的輸出將是:

Donec gravida link?=c_01 quam nec pulvinar link?=c_02 facilisis. 
Donec sed consectetur link?=c_03 lectus 

請注意在preg_replace)第二個參數(,它是$matches[1]

+0

解釋通常比代碼更有幫助。 – rlemon

+0

這是很好的解決方案,但事實上,這兩個功能將用於生成動態鏈接。我設法得到「c」和「01,02,03」的值。因此,如果網站設置爲使用mod_rewrite,則鏈接將生成http://sitenme.com/c_01-title-1.html,http://sitenme.com/c_02-title-2.html和http:// sitenme .COM/c_03標題 - 3.html。但不是,鏈接將生成http://sitenme.com/comment.php?id=1,http://sitenme.com/comment.php?id=2和http://sitenme.com/comment.php ?id = 3

所以我想我必須使用上面的2個函數。 –

+0

@JimJerry,是否要將「sitenme.com/c_01-title-1.html」,「sitenme.com/c_02-title-2.html」轉換爲「sitenme.com/comment.php?id=1,sitenme」。 com/comment.php?id = 2'? – srain

0

下面是一個很好的可回收的函數 - 你可以簡單地使用它'通過只傳遞一個字符串給它,或者你也可以傳遞一個替換和一個模式。

function ReplaceChar($string, $replace = "#*___*#", $pattern = "/\#\*(.*?)\*\#/"){ 

    $new = preg_replace($pattern, $replace, $string); 

    return $new; 

} 

$string = "Donec gravida #*___*# quam nec pulvinar #*c_02*# facilisis. Donec sed consectetur #*c_03*# lectus."; 

echo ReplaceChar($string); 

這將導致:

Donec gravida #*___*# quam nec pulvinar #*___*# facilisis. Donec sed consectetur #*___*# lectus.