5

i want to allow all utf8 character but want to replace non utf8 with space or -

this is the string

Punjab me 1Train k niche 100 Sardar aa gaye..

99 Mar gaye...

1 Bach gaya

whatever i do it is not inserting after train

its trimming at train

i have tried these two links

i want complete utf8 range to insert in mysql and replace the non utf8 with either space or -

$string = preg_replace('/[^(\x20-\x7F)]*/','', $string);

works for above case but when i use hindi/chinese it replaces that too.so i cant use above code

Community
  • 1
  • 1
james
  • 85
  • 7

2 Answers2

6

Try this: use only modifier u for Unicode

    $re = "/[^(\\x20-\\x7F\\n)]+/u";
    $str = "Punjab me 1Train k niche 100 Sardar aa gaye..\n\n99 Mar gaye...\n\n1 Bach gaya";
    $subst = "";

    $result = preg_replace($re, $subst, $str);

Live demo

Ahosan Karim Asik
  • 3,219
  • 1
  • 18
  • 27
0

Try this: Use str_replace() function:

$str = "Punjab me 1Train k niche 100 Sardar aa gaye..";
$rpl_arr = array('', '');
$srch_arr = array(' ', ' ');
$result = str_replace($rpl_arr, $srch_arr);
Bhavin Patel
  • 121
  • 2
  • 6