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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示