1

I'm trying to replace the value of Ñ in a preg_replace. I have this code:

<?php
$name = 'AVENDAÑO, PAULVIC T.';
$match = preg_replace('/[^A-Z]/','N',$name);

echo '<pre>';
print_r($match);
echo '</pre>';
?>

Though variable $name holds AVENDAÑO, PAULVIC T. Still it displays AVENDAÑO, PAULVIC T.

But when I try to use the code above it replaces all non-characters to N.

I know there's something wrong with my regex or does anyone knows any alternative to solve these problem regarding character formatting?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Vainglory07
  • 5,073
  • 10
  • 43
  • 77

2 Answers2

2

The best way to do this is to use iconv http://php.net/manual/en/function.iconv.php

echo iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $name);
Vlad Preda
  • 9,780
  • 7
  • 36
  • 63
  • this results to AVENDA~NO, PAULVIC T. – Vainglory07 Jan 10 '13 at 16:39
  • Not quite sure what the first / second params should be, check the PHP documentation for better information, I used this functionality rarely, but it's the best to use, since you need to write a few hundred str_replace() for every special char :) – Vlad Preda Jan 14 '13 at 07:47
2

Use str_replace instead.

<?php
$name = 'AVENDAÑO, PAULVIC T.';
$match = str_replace('Ñ','N',$name);

echo $match;
?>
DaveyBoy
  • 2,928
  • 2
  • 17
  • 27
  • Do you think there is still a way to display Ñ on screen correctly? Suppose that i want str_replace('Ñ','Ñ',$name); – Vainglory07 Jan 10 '13 at 16:41
  • If this is in a web page, check that the code page that is used is correct. UTF-8 is probably the one you want to use: `` should sort it out. If it's in a shell session, check the code page there too. – DaveyBoy Jan 10 '13 at 17:00