Geronimo89.dk

A blog about me, my doings and everything I think deserves attention.

PHP explode and implode

I think PHP is one of the few place where you can explode and implode without being a terrorist but still have fun with it. At least explode() is one of my favourite functions in PHP :).

For this little quick tip I actually have a quite practical use case I stumbled upon a couple of days ago. The jQueryUI datepicker widget. It lets the user pick a date, which you automatically can insert into a form. You can define different formats like day day, month month, year year and so on, whatever makes you happy, but to save it in a MySQL database you might want to convert it to the typical yyyy-mm-dd format. For that we spread the string we pull from the form at 2 different places (where the datepicker sets the slashes).

explode()

Exploding may sound a little extreme in the beginning, but it’s just a function to split a string into an array at certain points.

$explodeable = '28/05/2011';
$exploded = explode('/',$explodeable);
print_r($exploded);

// generates output:

Array ( [0] => 28 [1] => 05 [2] => 2011 )

implode()

Now, we still have to get the year, month and day together again to put it in a MySQL DATE or DATETIME field, so let’s look at the solutions we have for that with implode. Implode just puts a string together from an array with or without a certain character between them.

$exploded = array_reverse($exploded);
print_r($exploded);

// generates output:
Array ( [0] => 2011 [1] => 05 [2] => 28 )

$imploded = implode('-', $exploded);
echo $imploded

// generates output:
2011-05-28

For this it works perfectly, but you can also chain the different values of an array as following, if array_reverse can’t do the job and you want to sort your entries manually. This solution gives you more flexibility, also with the characters in between the split values.

$chained = $exploded[2].'-'.$exploded[1].'-'.$exploded[0];
echo chained;

// generates output:
2011-05-28

Thanks for reading my little quick tip on date conversion between the jQueryUI and MySQL, have a nice time everybody icon smile PHP explode and implode

Only 1 comment so far

phpBB3: change standard language

Sometimes around the internet it happens. Somebody sets up a board real quick and doesn’t add the right language pack, setting it as standard. So when you want to change the standard board language in a phpBB3, it will not apply the feature before users are logged in and logged out again or they edit it manually.

UPDATE phpbb_users
SET user_lang = "de"
WHERE user_type = 2;

Your table doesn’t need to be called phpbb_users, but maybe has a prefix you defined yourself. Check in the settings of your board or with phpmyadmin. You should also check user_type and see the IDs available there so you can run the command for all groups in there.

Links:

Give me the first comment

PHPbb3: Standardsprache nachträglich ändern

Es soll ja schon einmal vor kommen, dass man ein Forum wie z.B. phpBB3 aufsetzt und das nicht gleich in der richtigen Sprache. Eine Menge Leute melden sich an und dann möchte man die Sprache ändern. Eigentlich kein Problem, davon abgesehen, dass ein Aus- und Einloggvorgang der Nutzer notwendig ist oder diese manuell die Sprache ändern müssen.

UPDATE phpbb_users
SET user_lang = "de"
WHERE user_type = 2;

user_type sollte vorher ausgelesen werden (man kann die Anzahl der Mitglieder ersehen und diese mit der Software abgleichen, ob man denn auch die richtigen Gruppen erwischt. Ansonsten Kommando einfach für Gruppen einzeln ausführen) um herauszufinden welche Gruppen IDs aus der Datenbank mit welchen im Board überein stimmen.
Links:

Give me the first comment