problem with str_replace

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • avion
    New Member
    • Feb 2008
    • 3

    problem with str_replace

    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

    Code:
    <?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's obvious that I'm repeating some stupid mistake .. but I can't figure it out
    Last edited by avion; Feb 17 '08, 04:11 PM. Reason: making it more readable..
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    You are saying "have trouble" with str_replace. Wat trouble exactly? What is your problem?

    Ronald

    Comment

    • avion
      New Member
      • Feb 2008
      • 3

      #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.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Look at the second part of your code[php] if ($skip_this !=1 ) {
        #echo "This would be transliterated: ",$word,"\n ";
        for($a=0;$a<cou nt($latin);$a++ ){ #transliterate latin letters to cyrillic
        $current_cyr=$c yril[$a];
        $current_lat=$l atin[$a];
        $output=str_rep lace($current_l at,$current_cyr ,$word);
        // 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_rep lace($latin,$cy ril,$word);
        }
        $input .= $output;[/php]
        Ronald

        Comment

        • avion
          New Member
          • Feb 2008
          • 3

          #5
          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.

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Glad you got it working. See you next time.

            Ronald

            Comment

            Working...