0

I am retrieving data from another website using the Dom Xpath Object

$oDomObject = $oDomXpath->query($query);

The external website has this encoding in the header:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

I retrieve a value which I store in a variable, $my_var. If I echo it, it prints "música" on my page. In addition, if I look at the html source code, it also appears without any encoding as "música". My wordpress website where I am displaying the data has this encoding in the header:

<!DOCTYPE html>
<html class="no-js" lang="en-US" prefix="og: http://ogp.me/ns#">
<head>
<meta charset="UTF-8">

However, if I do the php comparison if($my_var=="música"), it does not return true. Any thoughts why? Does it have to do with the different heading encodings? I suspect it has to do with the special character "ú". Please help. I really need to get this done. Thank you very much in advance.

karlosuccess
  • 843
  • 1
  • 9
  • 25
  • 2
    `=` you are using assignment operator inside the `if()` instead of `==` comparison operator – user1978142 Jun 17 '14 at 01:55
  • Good catch. I am actually using the right syntax in my code. I just misspelled here. I just corrected it. Any ideas on how I can get this solved? – karlosuccess Jun 17 '14 at 02:31
  • oh okay just a typo. are you sure `$my_var` really contains `música`. should be true, if thats the case. check `var_dump($my_var)` – user1978142 Jun 17 '14 at 02:39
  • I will check that and get back... thanks for the suggestion. – karlosuccess Jun 17 '14 at 02:43
  • wow thats bizarre indeed, just shootin' in the dark, could you try `if(trim($my_var) == 'música') {` – user1978142 Jun 17 '14 at 02:44
  • When I do `var_dump($my_var)` it prints `string(7) "música"` although there are only 6 chars. Any other thought please?? – karlosuccess Jun 17 '14 at 02:47
  • `var_dump(trim($my_var))` prints `string(7) "música"` although there are only 6 chars. Is the special char `ú` worth two or something? Any other thought please?? – karlosuccess Jun 17 '14 at 02:50
  • yes i think this is not a bug, though i cant seem to search it thru the manual about accented characters – user1978142 Jun 17 '14 at 02:53
  • just shootin in the dark again, try `$my_var = iconv("iso-8859-1", "UTF-8", $my_var);`, then `if($my_var == 'música') {` – user1978142 Jun 17 '14 at 02:56
  • this sheds some light about the count of characters (why its seven), [link](http://stackoverflow.com/questions/11034058/strlen-and-utf-8-encoding) – user1978142 Jun 17 '14 at 03:02
  • The value has change, now it is not `música` anymore. Now it is `Irán`. `echo iconv("iso-8859-1", "UTF-8", trim($my_var))` outputs `Irán` ... and `var_dump($my_var)` outputs `string(5) "Irán"` ... any thought? – karlosuccess Jun 17 '14 at 03:08
  • wow, talk about messing it up, do you have a link of the origin of this document? – user1978142 Jun 17 '14 at 03:10
  • www.rojadirecta.me ... the source code has the word `Irán` ... – karlosuccess Jun 17 '14 at 03:15
  • for the meantime, my last idea is to use `utf8_encode()` on `$my_var`, i'l check the link. – user1978142 Jun 17 '14 at 03:16
  • `utf8_encode($my_var)` prints `Ir�n` .. thank you for your help .. please see if you can think of anything else – karlosuccess Jun 17 '14 at 03:26
  • actually, i just messed up the your comment box, i dont know why `$my_var` which contains `música` cant be true inside `if($my_var == 'música') {`. sorry but im at a loss. – user1978142 Jun 17 '14 at 03:28

0 Answers0