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?!

1
  • 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. Commented Jun 11, 2012 at 11:46

3 Answers 3

2

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

Try:

array_map(array($this, $this->process_elements), $data);
Sign up to request clarification or add additional context in comments.

1 Comment

that has answered my question
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

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.