user3434356
user3434356

Reputation: 21

php preg_replace fo danish char's

I did search a lot for this but not come to anything that works

Case:

I have a php page that show delivery info from a pizza delivery, everything works except the special danish char's: æ, ø and å and their capital counteparts Æ. Ø and Å

they come out ok on screen but on a serial thermal printer they do not and i want to replace these 3 char's with the code that tell the printer what to print.

To make it hard these 3 can show up anywhere INSIDE a word, one example is my streetname: Sandkæret followed by house no. The streetname could also be 2 words.

I can get the replace to work if these char's are on their own, but not if they are inside a word.

So far i have used:

$string = $row['delivery_street_address'];

$patterns = array();
$patterns[0] = '*å*';
$patterns[1] = '*æ*';
$patterns[2] = '*ø*';


$replacements = array();
$replacements[3] = '/x7D';
$replacements[2] = 'X';
$replacements[1] = '/x7C';


echo str_replace($patterns, $replacements, $string);

also tried this one:

echo preg_replace("/([æ])/", "<span class=\"initial\">$1</span>", $string);

but still no go

could a kind soul help out a NOOB + Newbie ?

Upvotes: 1

Views: 216

Answers (2)

Rob W
Rob W

Reputation: 9142

Replacing str_replace with preg_replace would work. Or @p.s.w.g's answer would work as well :)

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149020

str_replace will work fine, just remove the *'s around the characters:

$patterns = array();
$patterns[0] = 'å';
$patterns[1] = 'æ';
$patterns[2] = 'ø';

Of course, you could also write it like this:

$patterns = array('å', 'æ', 'ø');
$replacements = array('/x7D', 'X', '/x7C');
echo str_replace($patterns, $replacements, $string);

Upvotes: 3

Related Questions