2017-06-02 69 views
1

只是評論: 在上一個問題中,我需要將值從第0列更改爲1或1更改爲0,具體取決於$myVar變量中正在解析的任何值,此處由用戶附加的解決方案計算器是理想的:如何用隨機選擇的數組元素替換子串?

$myWords=array(
    array('funny','sad'), 
    array('fast','slow'), 
    array('beautiful','ugly'), 
    array('left','right'), 
    array('5','five'), 
    array('strong','weak') 
); 

// prepare values from $myWords for use with strtr() 
$replacements=array_combine(array_column($myWords,0),array_column($myWords,1))+ 
       array_combine(array_column($myWords,1),array_column($myWords,0)); 
echo strtr($myVar,$replacements); 

輸入/輸出:

$myVar='I was beautiful and strong when I was 5 now I\'m ugly and weak'; 
//outputs: I was ugly and weak when I was five now I'm beautiful and strong 

這個問題:

但是,在情況下,您是否看到具有多個選項的陣列來進行切換?如何讓系統在多個選項中進行任何單詞的交換,但是不會冒着用$myVar中最初出現的關鍵字/詞來執行「回聲」的風險,即稱爲證券交易所操作的關鍵字/單詞?

$myWords=array(
    array('funny','sad','very sad','hyper funny'), 
    array('fast','slow','faster','very slow'), 
    array('beautiful','Studious man','great beauty','very hardworking'), 
); 

$myVar = 'That man is really fast and very hardworking'; 

如何使其他選項中系統自動選擇,但它從交易所或蘭特或mt_rand等等,負責調用操作的按鍵排除:快速,用力,以免運行$myVar的風險不會改變。

可能的預期輸出:

$myVar = 'That man is really faster and Studious man'; 

fast不得被fastvery hardworking更換不得被very hardworking所取代。

+0

我可能會用'array_reduce()'建議'array_map()'。 –

+0

@mickmackusa哦,是的,我很抱歉,我很傻,對不起,我需要提醒你做正確的事情。 :-) –

回答

1

我想這是你正在試圖做...

檢查從各子陣列隨機選擇的字符串。 如果匹配,您想用相同子數組中的任何其他單詞替換它。 如果沒有匹配,請轉到下一個子數組。

$myWords=array(
    array('funny','sad','very sad','hyper funny'), 
    array('fast','slow','faster','very slow'), 
    array('beautiful','Studious man','great beauty','very hardworking'), 
); 

foreach($myWords as $words){ 
    // randomize the subarray 
    shuffle($words); 
    // pipe-together the words and return just one match 

如果(的preg_match( '/ \ B \ K'.implode(' |」。,$字) '\ B /',$ myVar的,$出來)){爲圖案=不正確的使用的\b

if(preg_match('/\K\b(?:'.implode('|',$words).')\b/',$myVar,$out)){ 
     // generate "replace_pair" from matched word and a random remaining subarray word 
     // replace and preserve the new sentence 
     $myVar=strtr($myVar,[$out[0]=>current(array_diff($words,$out))]); 
    } 
} 
echo $myVar; 

如果:

$myVar='That man is really fast and very hardworking'; 

那麼輸出可以是下列任一多:

That man is really faster and great beauty 
That man is really slow and Studious man 
etc... 

實際上,無論發生什麼樣的隨機替換,輸出都不會與輸入相同。

這裏是demo link


這是preg_match_all()版本:

$myWords=array(
    array('funny','sad','very sad','hyper funny'), 
    array('fast','slow','faster','very slow'), 
    array('beautiful','Studious man','great beauty','very hardworking'), 
); 

$myVar='The slow epic fail was both sad and funny'; 

foreach($myWords as $words){ 
    $replacepairs=[]; // clear array 

如果(preg_match_all( '/ \ B \ K'.implode(' |」,$的話)。'\ B /',$ myVar的,$出來)){爲圖案=不正確使用的\b

if(preg_match_all('/\K\b(?:'.implode('|',$words).')\b/',$myVar,$out)){ // match all occurences 
     foreach($out[0] as $w){ 
      $remaining=array_diff($words,[$w]); // find the remaining valid replacment words 
      shuffle($remaining);     // randomize the remaining replacement words 
      $replacepairs[$w]=current($remaining); // pluck first value from remaining words 
     } 
     $myVar=strtr($myVar,$replacepairs);  // use replacepairs on sentence 
    } 
} 
echo $myVar; 

可能的輸出:

The faster epic fail was both hyper funny and hyper funny 
The very slow epic fail was both very sad and hyper funny 
The fast epic fail was both funny and very sad 
etc... 

這裏是demo link

+0

@ArianeMartinsGomesDoRego解決方案是否需要在子陣列中啓用多個替換?考慮這個字符串:「晚間新聞既悲傷又有趣。」我是否需要使用preg_match_all()來替換'sad'和'funny',否則每個子數組中只會有一個匹配單詞? – mickmackusa

+0

我的第一種使用'preg_match()'的方法只允許從每個子數組中進行一次替換。我使用'preg_match_all()'的第二種方法允許從每個子陣列進行多次替換。你在同一個子陣列中看到'悲傷'和'滑稽',但它們在句子中出現兩次。第二種方法取代它們兩個。 – mickmackusa

+0

哦,是的,我現在已經看到你的答案了,我已經測試了,謝謝:-) –

相關問題