By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,303 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,303 IT Pros & Developers. It's quick & easy.

problem with str_replace

P: 3
I'm having problem with str_replace for some reason I don't understand.
The function should take all the content of a page .. skip some words, and transliterate from latin to cyrillic all the others. Here goes the code

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $cyril=array("Љ","Њ","А","Б","В","Г","Д","Ђ","Е", "Ж","З","И","Ј","К","Л","М","Н","О", "П", "Р","С","Ш","Т","Ћ","У","Ф","Х","Ц","Ч", "Џ", "Ш","љ","њ","а","б","в","г","д","ђ", "е","ж", "з","и","ј","к","л", "м","н","о","п", "р","с","ш","т","ћ","у","ф","х","ц","ч", "џ","ш");
  3. $latin=array("LJ","NJ","A","B","V","G","D","Đ","E", "","Z","I","J","K","L","M","N","O", "P", "R","S","","T","Ć","U","F","H","C","Č", "D","","lj","nj","a","b","v","g","d","đ",  "e","","z", "i","j","k","l","m","n","o","p","r", "s","","t","ć","u","f","h","c","č","d","");
  4. $eng=array("engleski","example");
  5. $content="srpski engleski primer example";
  6. $ar=preg_split("/\b/",$content);
  7. foreach ($ar as $word) {
  8. $skip_this=0;
  9. $output = $word;
  10.     for($i=0;$i<count ($eng);$i++){ #if found $eng skip transliteration
  11.         $curr_en=$eng[$i];
  12.         if ($word == $curr_en) {
  13.              $skip_this = 1;
  14.             #echo "I found word to skip: ", $output,"\n";
  15.             }
  16.         }
  17.         if ($skip_this !=1 ) {
  18.            #echo "This would be transliterated: ",$word,"\n";
  19.            for($a=0;$a<count($latin);$a++){ #transliterate latin letters to cyrillic
  20.            $current_cyr=$cyril[$a];
  21.            $current_lat=$latin[$a];
  22.            $output=str_replace($current_lat,$current_cyr,$word);
  23.         }
  24.     }
  25.     $input .= $output;
  26.     }
  27.  
  28. echo "orginal: $content transliterated: $input \n";
  29. ?>

It's obvious that I'm repeating some stupid mistake .. but I can't figure it out
Feb 17 '08 #1
Share this Question
Share on Google+
5 Replies


ronverdonk
Expert 2.5K+
P: 4,258
You are saying "have trouble" with str_replace. Wat trouble exactly? What is your problem?

Ronald
Feb 17 '08 #2

P: 3
It doesn't change any of the letters .. It should transliterate from latin to cyrillic, word by word. But when it comes to str_replace it doesn't replace any of the letters for some reason.
Feb 17 '08 #3

ronverdonk
Expert 2.5K+
P: 4,258
Look at the second part of your code[php] if ($skip_this !=1 ) {
#echo "This would be transliterated: ",$word,"\n";
for($a=0;$a<count($latin);$a++){ #transliterate latin letters to cyrillic
$current_cyr=$cyril[$a];
$current_lat=$latin[$a];
$output=str_replace($current_lat,$current_cyr,$wor d);
// YOU MUST SAVE $WORD SOMEWHERE OR IT WILL BE OVERWRITTEN IN THE NEXT ITERATION OF THE FOR LOOP
}
}
$input .= $output;[/php]
Variable $output is not saved, so the result of the for loop is ony ONE letter in $output.
Something else, you can do a str_replace using arrays, so you could make the translate for the entire $word as:
[php] if ($skip_this !=1 ) {
#echo "This would be transliterated: ",$word,"\n";
#transliterate latin letters to cyrillic
$output=str_replace($latin,$cyril,$word);
}
$input .= $output;[/php]
Ronald
Feb 17 '08 #4

P: 3
Ok thnx for quick response, it works now.

I tried that on the begining, but obviosly I didn't get it right.
After what, I just made mess with coding in wrong direction.
Feb 17 '08 #5

ronverdonk
Expert 2.5K+
P: 4,258
Glad you got it working. See you next time.

Ronald
Feb 17 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.