Jump to content

Change letters in a string


JJohnsenDK

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.