JJohnsenDK Posted March 17, 2008 Share Posted March 17, 2008 Hey How do i change letters in a string. And not just one letter. For example this string "Småbørn". Here are to none english letters which i want to change to english letters so i get: "Smaborn". How do i do this? $name = "Småbørn"; $len = strlen($name); $returnName = ""; for($i=0;$i<$len;$i++){ $letter = substr($name, $i, 1); echo "d"; if(strpos($letter, "å")){ $returnName .= str_replace("å", "a", $letter); }elseif(strpos($letter, "ø")){ $returnName .= str_replace("ø", "o", $letter); }elseif(strpos($letter, "å")){ $returnName .= str_replace("å", "a", $letter); }else{ $returnName .= $letter; } } echo $returnName; but it sucks. It doesnt work. Any ideas? Link to comment https://forums.phpfreaks.com/topic/96611-change-letters-in-a-string/ Share on other sites More sharing options...
lemmin Posted March 17, 2008 Share Posted March 17, 2008 You should probably use preg_replace(). http://us.php.net/manual/en/function.preg-replace.php Good luck! Link to comment https://forums.phpfreaks.com/topic/96611-change-letters-in-a-string/#findComment-494398 Share on other sites More sharing options...
JJohnsenDK Posted March 18, 2008 Author Share Posted March 18, 2008 sry for late reply... preg_replace doesnt help me much here? how should this be better than str_replace? Link to comment https://forums.phpfreaks.com/topic/96611-change-letters-in-a-string/#findComment-494682 Share on other sites More sharing options...
berridgeab Posted March 18, 2008 Share Posted March 18, 2008 I don't understand why you have an IF statement and a WHILE loop within your code. If your just looking to replace them characters with English characters then why not just use the str_replace() on its own? i.e $name = "Småbørn"; $name = str_replace("å", "a", $name); $name = str_replace("ø", "o", $name); $name = str_replace("å", "a", $name); echo $name; This worked fine for me. Link to comment https://forums.phpfreaks.com/topic/96611-change-letters-in-a-string/#findComment-494701 Share on other sites More sharing options...
JJohnsenDK Posted March 18, 2008 Author Share Posted March 18, 2008 also works for me... thanks alot mate! Link to comment https://forums.phpfreaks.com/topic/96611-change-letters-in-a-string/#findComment-494769 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.