Archive for the ‘PHP’ Category

Articles

Convert XML Object to Array in PHP

In PHP on October 15, 2011 by patrellery

Here’s a very simple way to convert an XML Object to an array in PHP.

$string = '
<user>
  <id>1234</id>
  <username>root</username>
  <password>helloWorld</password>
  <access>
    <admin>0</admin>
    <write>1</write>
    <read>1</read>
  </access>
  <messages>
    <message>Message 1</message>
    <message>Message 1</message>
  </messages>
</user>';

$xml = json_decode(json_encode((array)simplexml_load_string($string)), TRUE);

easy no?

Articles

Find user’s timezone with PHP and Ajax

In PHP on October 14, 2011 by patrellery

Here’s a simple function to find out what is the user’s timezone in PHP


<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
if("".length==0){
var visitortime = new Date();
var visitortimezone = "GMT " + -visitortime.getTimezoneOffset()/60;
$.ajax({
type: "GET",
url: "http://domain.com/timezone.php",
data: 'time='+ visitortimezone,
success: function(){
location.reload();
}
});
}
});

and then on your php file:
<?php
session_start();
$_SESSION['time'] = $_GET['time'];
?>

Articles

How to clear redis keys

In PHP on October 2, 2011 by patrellery Tagged:

The other day we found a big issue on our development platform and we had to clear Redis memory/Keys to make sure we re-import valid data to our database.
Read More »

Articles

Split string by delimiter in PHP

In PHP on September 29, 2011 by patrellery Tagged:

Here’s a quick tip to split a string using the pipe (|) delimiter in PHP. Let’s say you have a log or some sort of non usual string you want to split, such as:

$str = 'A|B\|B|C\\|D\\\|D';

You want this to look like:

$array[0] => 1
$array[1] => 2\|2
$array[2] => 3\\
$array[3] => 4\\\|4

Read More »