0

I am trying to use this function to convert danish characters to utf

private function process_elements($element){
   $element=  strtolower ( trim ( $element ) );
   $element=  mysql_real_escape_string($element);
    return utf8_encode($element);
}

my array is used without the encoding like this:

Array ( [0] => Desktop [1] => imlive [2] => dk [3] => Danish [4] => Denmark [5] => http://www.google.dk/search?ie=UTF-8&oe=UTF-8&hl=da&q=imlive&adtest=on&gl=DK&glp=1&ip=0.0.0.0&pws=0&noj=1&nomo=1 [6] => ImLive - Fr�kke cam-piger [7] => Tusindvis af rigtige amat�rer live! [8] => Fra private hjem og sovev�relser [9] => dk.imlive.com [10] => ImLive - Fr�kke cam-piger [11] => dk.imlive.com [12] => Tusindvis af rigtige amat�rer live! Fra private hjem og sovev�relser [13] => ImLive - Fr�kke cam-piger - Tusindvis af rigtige amat�rer live! [14] => dk.imlive.com [15] => Fra private hjem og sovev�relser [16] => ImLive - Fr�kke cam-piger [17] => Tusindvis af rigtige amat�rer live! Fra private hjem og sovev�relser [18] => dk.imlive.com )

here is my implementation:

array_map('self::process_elements', $data);

the function is in a class...

later i set such queries. for example:

        mysql_query("SET NAMES 'utf8'");
         $query="INSERT INTO advert
                 SET device='$device',
                 keyword='$keyword',
                 google_domain='$google_domain',
                 language='$language',
                 country='$country',
                 check_url='$check_url',
                 task_id=$this->task_id";

       mysql_query($query) or die(myql_error());

but it throws exceptions that the a characters are wrong.. because before i inpu thtem, they are not utf.. why the array map function doesnt work?!

rid
  • 61,078
  • 31
  • 152
  • 193
Dmitry Makovetskiyd
  • 6,942
  • 32
  • 100
  • 160
  • 1
    As an aside, you should not change the encoding of your string *after* you have `mysql_real_escaped` it. `mysql_real_escape_string` is always the very last function to apply before you concatenate the value into the query. – deceze Jun 11 '12 at 11:46

3 Answers3

2

Take a look at this thread: Passing object method to array_map()

Try:

array_map(array($this, $this->process_elements), $data);
Community
  • 1
  • 1
472084
  • 17,666
  • 10
  • 63
  • 81
1

Quote from php.net

If you need to call a static method from array_map, this will NOT work:

<?PHP array_map('myclass::myMethod' , $value); ?>

Instead, you need to do this:

<?PHP array_map( array('myclass','myMethod') , $value); ?>

It is helpful to remember that this will work with any PHP function which expects a callback argument.

http://php.net/manual/de/function.array-map.php

sofl
  • 1,024
  • 8
  • 13
1

You cannot change initial values in that array, because array_map() returns an new array with changed values, it is not passed by reference.

$newArray = array_map('self::process_elements', $data);
Petro Popelyshko
  • 1,353
  • 1
  • 15
  • 38