Tag Archives: php
Convert variable to string in PHP

Convert variable to string in PHP PHP is a loosely typed language and assigns types to variables depending what is assigned to it. Sometimes you will want to transform a given variable into a string format and obtain a similar output with “ToString()” method in Java or .NET. This could be done easily with one of the following
How to explode a string on upper case characters

How to explode a string on upper case characters Problem You have a string with the following value “thisStringMustBeSplittedAfterUpperCase” and you want to obtain every word separately in an array, splitted after upper case. Solution
1 |
$chunks = preg_split('/(?=[A-Z])/', $string, -1, PREG_SPLIT_NO_EMPTY); |
Output The output of the $chunks array obtained with a var_dump($chunks) will be:
1 2 3 4 5 6 7 8 9 |
array (size=8) 0 => string 'this' (length=4) 1 => string 'String' (length=6) 2 => string 'Must' (length=4) 3 => string 'Be' (length=2) 4 => string 'Splitted' (length=8) 5 => string 'After' (length=5) 6 => string 'Upper' (length=5) 7 => string 'Case' (length=4) |
Solution in a function
1 2 3 4 5 6 |
function upperCaseSplit($string){ return preg_split('/(?=[A-Z])/', $string, -1, PREG_SPLIT_NO_EMPTY); } $string = 'thisStringMustBeSplittedAfterUpperCase'; var_dump (upperCaseSplit($string) ); |
How to strip all spaces out of a string in php

How to strip all spaces out of a string in php You have a string stored in a PHP variable, and you want to eliminate all whitespaces or spaces from that string with PHP, not only from the beginning or the end of the string. Scenario
1 2 3 4 5 6 |
<?php //You want to strip all spaces from the below string and obtain "thisisjustastring" $firstString= 'this is just a string'; ?> |
Solution for spaces
1 2 3 4 5 6 7 |
<?php $firstString= 'this is just a string'; $newString = str_replace(' ', '', $firstString); echo $newString; //outputs "thisisjustastring" as a string ?> |
Solution for whitespaces
1 2 3 4 5 6 7 |
<?php $firstString= 'this is just a string'; $newString = preg_replace('/\s+/', '', $firstString); echo $newString //outputs "thisisjustastring" as a string ?> |
How to use PHP namespaces

PHP namespaces Before we dig in into namespaces, we have to understand what they are, and how they are used. A simple analogy can be made between PHP namespaces and a filesystem. They work quite the same and apply the same principle. Imagine the following directory structure:
1 2 3 4 5 6 7 8 9 |
/myLocalSpace /home foo.txt /work bar.txt |
Everything clear. You have a “foo.txt” file in the
Build multilanguage websites with gettext

Build multilanguage websites with gettext Have you ever tried to build a multilanguage website using php ? Whether you did it or not, you should know that this thing requires some thinking before you start coding. I will present you a very simple, fast and scalable solution for this. What is gettext ? Gettext is a
5 of the best PHP frameworks for programmers

A bit about php frameworks This is a topic that is not very well understood by people who jump into development pool. PHP frameworks help to promote rapid application development, which saves you time, helps build more stable applications, and reduces the amount of repetitive coding for developers. Frameworks can also help beginners to build more
Best practices to optimize code performance in php

Best practices to optimize code performance in php We developers, should search constantly for best practices when writing code. In this article I will focus on how to can write better and faster php code. Keep in mind also that people get carried away with performance. I personally prefer to design for flexibility first, and worry
Install memcached daemon / Install memcached extension using pecl

A little about Memcached Memcached is a service that allows you to store in memory key-value data like strings or objects, from results like databases queries, API calls and drastically accelerating queries and reducing the load on your server. Memcached is being used by major websites like Youtube, Twitter, Digg, WordPress.com and many many more.