Заменить целые слова в строке без замены замен ⇐ Php
Заменить целые слова в строке без замены замен
I need to replace each word only once, and vice versa. To do that, I used a code that does not work, and I can not find an answer to my question.
Input:
hello w1 w2 w12 new1 new12 new2
Expected output:
hello new1 new2 w12 w1 new12 w2
I need words / phrases into the text to replace.
w1 replace with new1
w12 unchanged
w2 replace with new2
new1 replace with w1
new12 unchanged
new2 replace with w2
Of course my text in Persian
My code is:
$string="hello w1 w2 w12 new1 new12 new2"; $fword= array("w1","w2"); $lword= array("new1","new2"); $cnt=0; $string=str_replace($fword,$lword,$string,$cnt); $string=str_replace($lword,$fword,$string,$cnt); echo "Change in string: $cnt
New String: $string "; But it is wrong
I also use this code:
$string="hello w1 w2 w12 new1 new12 new2"; $fword= array("w1","w2","new1","new2"); $lword= array("new1","new2","w1","w2"); $cnt=0; $string=str_replace($fword,$lword,$string,$cnt); echo "Change in string: $cnt
New String: $string ";
Источник: https://stackoverflow.com/questions/432 ... placements
I need to replace each word only once, and vice versa. To do that, I used a code that does not work, and I can not find an answer to my question.
Input:
hello w1 w2 w12 new1 new12 new2
Expected output:
hello new1 new2 w12 w1 new12 w2
I need words / phrases into the text to replace.
w1 replace with new1
w12 unchanged
w2 replace with new2
new1 replace with w1
new12 unchanged
new2 replace with w2
Of course my text in Persian
My code is:
$string="hello w1 w2 w12 new1 new12 new2"; $fword= array("w1","w2"); $lword= array("new1","new2"); $cnt=0; $string=str_replace($fword,$lword,$string,$cnt); $string=str_replace($lword,$fword,$string,$cnt); echo "Change in string: $cnt
New String: $string "; But it is wrong
I also use this code:
$string="hello w1 w2 w12 new1 new12 new2"; $fword= array("w1","w2","new1","new2"); $lword= array("new1","new2","w1","w2"); $cnt=0; $string=str_replace($fword,$lword,$string,$cnt); echo "Change in string: $cnt
New String: $string ";
Источник: https://stackoverflow.com/questions/432 ... placements
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Замените целые ноты в строке, используя массив «найти» и массив «заменить».
Anonymous » » в форуме Php - 0 Ответы
- 11 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Гсон. Десериализовать целые числа как целые числа, а не как двойные числа.
Anonymous » » в форуме JAVA - 0 Ответы
- 96 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Заменить слова в тексте с помощью словаря Python (заменить ключ его значением)
Anonymous » » в форуме Python - 0 Ответы
- 140 Просмотры
-
Последнее сообщение Anonymous
-