简体   繁体   中英

how can i stop some words/letters from being swapped in 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.

About first part. You should replace more specific word first. ie "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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM