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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.