problem with str_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jet-plane
Forum Newbie
Posts: 3
Joined: Sun Feb 17, 2008 9:20 am

problem with str_replace

Post by jet-plane »

I have trouble with str_replace for some reason I don't understand.
The funcion should take all the content of a page .. skip some words, and transliterate from latin to cyrillic all the others. Here goes the code

Code: Select all

<?php
$cyril=array("?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?");
$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ž","š");
$eng=array("engleski","example");
$content="srpski engleski primer example";
$ar=preg_split("/\b/",$content);
foreach ($ar as $word) {
$skip_this=0;
$output = $word;
    for($i=0;$i<count ($eng);$i++){ #if found $eng skip transliteration
        $curr_en=$eng[$i];
        if ($word == $curr_en) {
             $skip_this = 1;
            #echo "I found word to skip: ", $output,"\n";
            }
        }
        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,$word);
        }
    }
    $input .= $output;
    }
 
echo "orginal: $content transliterated: $input \n";
?>
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.
It's obvious that I'm repeating some stupid mistake .. but I can't figure it out
jet-plane
Forum Newbie
Posts: 3
Joined: Sun Feb 17, 2008 9:20 am

Re: problem with str_replace

Post by jet-plane »

Ok I found the solution with help on some other forum ..

Code: Select all

     if ($skip_this !=1 ) {
             #echo "This would be transliterated: ",$word,"\n";
             #transliterate latin letters to cyrillic
             $output=str_replace($latin,$cyril,$word);
          }
          $input .= $output;
Post Reply