PHP: XML to Array

A simple post for today. A HUGE thanks to @gaarf for his post! :)

<?
/**
 * convert xml string to php array - useful to get a serializable value
 *
 * @param string $xmlstr
 * @return array
 * @author Adrien aka Gaarf
 */
function xmlstr_to_array($xmlstr) {
  $doc = new DOMDocument();
  $doc->loadXML($xmlstr);
  return domnode_to_array($doc->documentElement);
}
function domnode_to_array($node) {
  $output = array();
  switch ($node->nodeType) {
   case XML_CDATA_SECTION_NODE:
   case XML_TEXT_NODE:
    $output = trim($node->textContent);
   break;
   case XML_ELEMENT_NODE:
    for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
     $child = $node->childNodes->item($i);
     $v = domnode_to_array($child);
     if(isset($child->tagName)) {
       $t = $child->tagName;
       if(!isset($output[$t])) {
        $output[$t] = array();
       }
       $output[$t][] = $v;
     }
     elseif($v) {
      $output = (string) $v;
     }
    }
    if(is_array($output)) {
     if($node->attributes->length) {
      $a = array();
      foreach($node->attributes as $attrName => $attrNode) {
       $a[$attrName] = (string) $attrNode->value;
      }
      $output['@attributes'] = $a;
     }
     foreach ($output as $t => $v) {
      if(is_array($v) && count($v)==1 && $t!='@attributes') {
       $output[$t] = $v[0];
      }
     }
    }
   break;
  }
  return $output;
}
?>

svn: Can’t create tunnel: The parameter is incorrect.

If you are trying to use SVN command line within Windows, and you have TortoiseSVN installed, you may run into this error:

svn: Can’t create tunnel: The parameter is incorrect.

I found out that I needed to set this property via Windows command line:

set SVN_SSH=path/plink.exe

However, the problem wasn’t resolved! The issue was that the first solution didn’t mention that the path had to be written using forward slashes [/]:

set SVN_SSH="C:\Program Files\TortoiseSVN\bin\TortoisePlink.exe" needed to be set SVN_SSH="C:/Program Files/TortoiseSVN/bin/TortoisePlink.exe"

Thanks to jcoles for the help; problem solved!

Nick

jQuery: “missing : after property id” Error

I didn’t realize how rusty one could get in just a week!

This is the code I was trying to use — a function I’ve used hundreds of times — and I kept getting the “missing : after property id” error.

$(document).ready({
});

Did you catch it? Neither did I ;-)

Well, according to the jQuery API the .ready() function takes a function — do you see my error now?

$(document).ready(function() {
});

Kudos to user113716 for [indirectly] pointing out my flaw, and with that silly bug I’m off to get another cup of caffeine.

Nick

TinyMVC: PDO & UTF-8 Character Set

When setting up the initial model for a TinyMVC project, I got the following error:

Message: Can’t connect to PDO database ‘mysql’. Error: SQLSTATE[HY000] [2019] Can’t initialize character set UTF-8 (path: /usr/share/mysql/charsets/)

After looking in /path/to/tinymvc/sysfiles/plugins/tinymvc_pdo.php I noticed the following on line 95:

    if(empty($config['charset']))
        $config['charset'] = 'UTF-8';

Since nothing appeared wrong, I did a quick Google search and found this bit of information:

Just tried your recommendation on the other server with php 5.3.6 and it worked with utf8 versus utf-8. Just to double check i tried it again with utf-8 and that didn’t work

So, armed with that knowledge, I updated /path/to/tinymvc/sysfiles/plugins/tinymvc_pdo.php to read as:


    if(empty($config['charset'])) {
        $config['charset'] = 'UTF8';
    }

Bingo! After saving the file and refreshing, the error was gone!

Nick

HTML and JavaScript: Safari and Loading Files

Today I experienced an odd issue with Safari. It appears as though the HTML attribute defer=”defer” is not translated by Safari. Below are two examples of code. The former was what worked in every major browser except Safari. The latter works in all browsers that I have tested.

<script type=”text/javascript” defer=”defer” src=”/js/file2.js”></script>
<script type=”text/javascript” defer=”defer” src=”/js/file3.js”></script>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js”></script>
<script type=”text/javascript” defer=”defer” src=”http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js”></script>
<script type=”text/javascript” defer=”defer” src=”/js/file1.js”></script>

<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js”></script>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js”></script>
<script type=”text/javascript” defer=”defer” src=”/js/file1.js”></script>
<script type=”text/javascript” defer=”defer” src=”/js/file2.js”></script>
<script type=”text/javascript” defer=”defer” src=”/js/file3.js”></script>

The reason that the former was in that order was because of the way that Zend loads files via prependFile() and appendFile(). Using the proper combination of the two functions, I was able to get them into the desired order.

Nick