Заменить целые слова в строке без замены заменPhp

Кемеровские программисты php общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Заменить целые слова в строке без замены замен

Сообщение Anonymous »


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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Php»