duanhuang1967 2013-05-14 22:18
浏览 39
已采纳

如何阻止某些单词/字母在PHP中交换?

I am using php to swap text from one textarea and display in another, and most of it works, but sadly some parts dont.

I am using str_ireplace and then so far, 60 different words, with many more to come. A couple of examples of my problem are:

old - auld
old man - gadgie

if you look above, the first word is the unswapped and the second is the new word. However, if i put old man in the text, it would come as "auld man" instead of "gadgie".

I know that this may be because i have declared "old-auld" before "old man - gadgie"

but another problem is that:

yes - aye
no - nar

Again, first word is original, second is after swap. If i were to write "yesterday" it would be displayed "ayeterday" or if i had "nose" it would be "narse"

Is there anyway to change ONLY "yes" on its own and not if it is part of another word?

And is there any way to have "old man" and "old" as separate replacements?

Hope that makes sense

My code for str_ireplace:

<form method="POST" action="post.php" >
<textarea name="status2" cols="50" rows="5"/>
<?php echo str_ireplace(array  ('old','awkward','all','again','behind','along','alright','hello','among','children','yes','child','kids','food','barnard castle','beer','book','blow','beautiful','bird','burst','brown','burn','boots','came','nice','cold','church','clothes','stick','dirty','clever','cloth','cow','crow','stool','crown','pigeon','dad','darlington','dead','do','devil','dont','do not','durham','down','drown','punch','ditch','eye','fall','trouble','condition','four','stupid','old man','go','going','jumper','give over','grandad','hold','get away','home'), 
array ('auld', 'aakwad', 'aall','agyen','ahint','alang','alreet','alreet','amang','bairns','aye','bairn','bairns','bait','barney','beor','beuk','blaa','bonny','bord','borst','broon','bourn','byeuts','cam','canny','card','chorch','claes','clag','clarty','clivvor','cloot','coo','craa','cracket','croon','cushat','da','darlo','deed','dee','deil','divvent','divvent','dorham','doon','droon','dunsh','dyke','Eee','faa','fash','fettle','fower','gaumless','gadgie','gan','gannin','ganzie','giveower','granda','haad','hadaway','hyem'),$status); ?> 

</textarea><br>

As i said, there are 60+ words.

I didn't add my code initially as i thought it may not have been relevant.

  • 写回答

1条回答 默认 最新

  • dongling0519 2013-05-14 22:24
    关注

    About first part. You should replace more specific word first. i.e "old man", and then "man" or use strtr

    <?php
    $trans = array("h" => "-", "hello" => "hi", "hi" => "hello");
    echo strtr("hi all, I said hello", $trans);
    ?>
    The above example will output:
    hello all, I said hi
    

    About the whole words. You may look to regexps (and \b in particularly)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘