473,385 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

problem with str_replace

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
5 2454
ronverdonk
4,258 Expert 4TB
You are saying "have trouble" with str_replace. Wat trouble exactly? What is your problem?

Ronald
Feb 17 '08 #2
avion
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
4,258 Expert 4TB
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
avion
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
4,258 Expert 4TB
Glad you got it working. See you next time.

Ronald
Feb 17 '08 #6

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

Similar topics

2
by: Phil Powell | last post by:
If $val is the following: ....Just revamped the site's Content Management Application I built.. so do bear in mind.. sorry! Phil stripslashes(htmlspecialchars($val)) should produce the...
1
by: Phil Powell | last post by:
<? class ErrorMsgCollection { var $name; var $mandatory; var $emptyErr; var $maxLength; var $maxLengthErr; var $minLength; var $minLenghtErr;
9
by: Hugo Coolens | last post by:
I need to use the str_replace function for a php-script which works together with html/javascript. In the script I have to perform things like: $arabic=str_replace ("'","\'", $row);...
1
by: duncan.lovett | last post by:
I am working on a graphical heading generator for a clients website as their server does not have the GD library or similar plugins for dynamic image generation. I have achieved the result...
1
by: marcomputers | last post by:
Hi, I'm trying to automatically update an existing rss podcast file using php and the str_replace function. Every time I add a media file, it should read the .rss file into a string, take the...
28
by: Ian Davies | last post by:
Hello I would appreciate some help from someone who has knowledge of working with css, php javascript and how they interact. Ive been working on a task for the last few days and have started to...
2
by: zek2005 | last post by:
Hi !!! I have the following php code <?php $db = mysql_connect("1", user, password) or die ("can΄t connect"); mysql_select_db(efemerides,$db); mysql_close(); ?>
4
by: d1srupt3r | last post by:
I am developing my form into a captcha secured form and I used header() function if the user didn't put the verification code properly and still when the user go back to the form page the filled he...
1
by: swethak | last post by:
hi, i run the below code i got the following error and also display output normal way instead of xml entries.plz help that what's the mistake in my code <? header("Content-type:...
4
pradeepjain
by: pradeepjain | last post by:
i had posted the same code in javascript area !! this i am posting bcos of different problem. as you can see in ma code there is a dropdown called sort ...its sorts the result by the value he...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.