2013-04-24 113 views
0

我想要:他有XXX擁有它。或者:他必須擁有XXX它。preg_replace - 替換某個字符

$string = "He had had to have had it."; 
echo preg_replace('/had/', 'XXX', $string, 1); 

輸出:

他XXX必須有它。

在'有'被替換的情況下是第一個。

我想使用第二和第三。沒有從右邊或左邊讀,什麼「preg_replace」可以做到這一點?

+0

或與您的XXX值 – 2013-04-24 11:53:15

+0

@ivanichi ...什麼是你想要的確切的輸入和預期的輸出,簡要地給出細節。 – 2013-04-24 14:32:51

+0

@Navnath,我想,他XXX必須擁有它或者他有XXX擁有它或者他必須擁有XXX它,語法不應該從右邊或左邊讀取,因爲必須確保序列的位置被替換爲同一個詞 – ivanichi 2013-04-24 14:52:17

回答

2
$string = "He had had to have had it."; 
$replace = 'XXX'; 
$counter = 0; // Initialise counter 
$entry = 2; // The "found" occurrence to replace (starting from 1) 

echo preg_replace_callback(
    '/had/', 
    function ($matches) use ($replace, &$counter, $entry) { 
     return (++$counter == $entry) ? $replace : $matches[0]; 
    }, 
    $string 
); 
+0

@mark,我已經試過你的語法,但錯誤意外T_FUNCTION,你能修復再試一次嗎?謝謝。 – ivanichi 2013-04-24 13:05:12

+0

那麼你使用的是什麼版本的PHP?這需要至少5.3.0,儘管你應該至少使用該版本 – 2013-04-24 13:15:24

+0

@mark,我的PHP版本5.2.6,以及,我會嘗試它 – ivanichi 2013-04-24 13:27:43

2

試試這個:

<?php 
function my_replace($srch, $replace, $subject, $skip=1){ 
    $subject = explode($srch, $subject.' ', $skip+1); 
    $subject[$skip] = str_replace($srch, $replace, $subject[$skip]); 
    while (($tmp = array_pop($subject)) == ''); 
    $subject[]=$tmp; 
    return implode($srch, $subject); 
} 
$test ="He had had to have had it.";; 
echo my_replace('had', 'xxx', $test); 
echo "<br />\n"; 
echo my_replace('had', 'xxx', $test, 2); 
?> 

CodeFiddle

0

試試這個

解決方案

function generate_patterns($string, $find, $replace) { 

// Make single statement 
// Replace whitespace characters with a single space 
$string = preg_replace('/\s+/', ' ', $string); 

// Count no of patterns 
$count = substr_count($string, $find); 

// Array of result patterns 
$solutionArray = array(); 

// Require for substr_replace 
$findLength = strlen($find); 

// Hold index for next replacement 
$lastIndex = -1; 

    // Generate all patterns 
    for ($i = 0; $i < $count ; $i++) { 

    // Find next word index 
    $lastIndex = strpos($string, $find, $lastIndex+1); 

    array_push($solutionArray , substr_replace($string, $replace, $lastIndex, $findLength)); 
    } 

return $solutionArray; 
} 

$string = "He had had to have had it."; 

$find = "had"; 
$replace = "yz"; 

$solutionArray = generate_patterns($string, $find, $replace); 

print_r ($solutionArray); 

輸出:

Array 
(
    [0] => He yz had to have had it. 
    [1] => He had yz to have had it. 
    [2] => He had had to have yz it. 
) 

我管理這個代碼試圖優化它。

+0

沒關係,但是謝謝你給出答案,也許其他人會需要這個答案。是的,這個語法取代所有,但不是第一個 – ivanichi 2013-04-24 14:40:36

+0

@ ivanichi ..如果你想只替換第一個字符串,那麼你的代碼工作,你能告訴我你想要什麼嗎? – 2013-04-24 14:44:44

+0

@ivanichi ...你說的 - 我想用第二和第三。沒有從右邊或左邊讀,什麼「preg_replace」可以做到這一點?意味着什麼 ? – 2013-04-24 14:47:28

0

可能不會贏得任何競賽德優雅與此,但很短:使用preg_replace_callback(),你可以保持找到事件的計數器,以確定是否簡單地更換自己

$string = "He had had to have had it."; 
echo strrev(preg_replace('/dah/', 'XXX', strrev($string), 1)); 
+0

沒關係,但是謝謝你給出答案,也許其他人會需要這個答案 – ivanichi 2013-04-24 14:40:54