Posts Tagged ‘Codes’
PHP string manipulation 2
Written by Codes Tips on July 31, 2009 – 2:34 pm -This is the second part of the how you can manipulate string in PHP.
Find length of a string
If you want to find out how many characters have a string you can use the function strlen. If you have to pass 1 argument as the string you wish you find out the length and it returns an integer.
Declaration: int strlen(string text)
Example:
Output: 31
Find a sub string inside a string
To find a first occurrence of a string inside other you should use the function strstr and pass it 2 arguments. First argument should be the string in which you wanna look and the second should be the string you search. It returns part of the first occurrence until the end of the string you search in.
Declaration: string strstr(string searchIn,string lookfor)
Example:
Output: strstr Function Test
Find a string inside other string(case insensitive)
To find an occurrence of a string inside another with case insensitive activated you should use the function stristr. It has the same parameters as the strstr, but it also finds case insensitive strings.
Declaration: string stristr(string SearchIn, string lookFor)
Example:
Output: StrStr Function Test
Find position of first occurrence of a string inside another
To find the position of the first occurrence of a string you can use the function strpos. You should pass two arguments. The first parameter is the string you wanna search in and the second is a the string you are looking for. You can specify an additional argument, an offset that indicates from where position the search should begin.
Declaration: int strpos ( string $SearchIn, mixed $SearchFor[, int $offset= 0 ] )
Example:
Output: 11
Find a position of a string inside another(case insensitive)
You can use the function strripos and it has the same arguments as strpos, but it searches for case insensitives.
Declaration: int strripos ( string $SearchIn, mixed $SearchFor[, int $offset= 0 ] )
How to return parts of a string
To return parts of a string you should use the substr function and pass it 2 arguments and 1 optional. The first argument is the string you wanna return a part of a string, second is the start position from where you wanna begin crop the string. You can also specify an argument with the length that the function should crop.
Declaration: string substr ( string $SearchIn , int $from [,int $length])
Example:
Output: is a substr
Insert after each end of line in string a html br tag
To insert html tag br after the new lines in a string you can use the function nl2br. Function takes one neccessary argument and one optional. First is a string where it will be inserted the br tag and the second one is a boolean that indicates if the replacement is a XHTML compatible line breaks(default value is TRUE).
Declaration: string nl2br(string $InsertIn [,bool $isXHTML= true])
Example:
Output:
This is a nl2br<br /> Function<br /> Test
PHP string manipulation - part 1 - PHP string manipulation - part 3
Tags: Codes, PHP
Posted in Codes, PHP/MYSQL | 2 Comments »
PHP cpanel scripts
Written by Codes Tips on June 1, 2009 – 8:50 am -Intro
To automate an action in PHP for cpanel it’s very easy. You just need to know some information about your cpanel. You can find this information on your browser link bar.
$cpanel_user - the username that you use to log into your cpanel
$cpanel_password - the password for cpanel.
$cpanel_host - your cpanel host. You can find this after you login to cpanel, right before ‘http://’ until ‘:’.
$cpanel_skin - cpanel skin. You can find this after ‘/frontend/’, default is ‘x’.
HTML Form
You can use this html forms to make the scripts for cpanel, you can add your own fields that you need.
<form action="cpaneladd.php" method="POST"> <table> <tr><td colspan="2" align="center"> <b>Cpanel info</b></td></tr><tr><td align="right">Cpanel host: </td><td align="left"> <input type="text" name="cpanelhost"></td></tr> <tr><td align="right">Cpanel username:</td><td align="left"> <input type="text" name="cpaneluser"></td></tr> <tr><td align="right">Cpanel password:</td><td align="left"> <input type="text" name="cpanelpass"></td></tr> <tr><td align="right">Cpanel skin:</td><td align="left"> <input type="text" name="cpanelskin" values="x"></td></tr> </table> </form>
I made a list with some useful functions that you can use for cpanel:
1. Adding a database in cpanel
$newdb = "db_name"; $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($curl, CURLOPT_URL, "http://$cpanel_user: $cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin /sql/adddb.html?db=$new_db"); $result=curl_exec ($curl); curl_close ($curl);
2. Adding a user to database in cpanel
$db_user = "dbuser"; $db = "database"; $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($curl, CURLOPT_URL, "http://$cpanel_user: $cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/ sql/addusertodb.html?user={$cpanel_user}_{$db_user} &db={$cpanel_user}_{$db}&ALL=ALL"); $result=curl_exec ($curl); curl_close ($curl);
3. Delete a user in cpanel
$userdb="userdb"; $curl = curl_init(); curl_setopt ($curl, CURLOPT_URL, "http://$cpanel_user: $cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/ sql/deluser.html?user=".$cpanel_user."_".$userdb); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result=curl_exec ($curl); curl_close ($curl);
4. Delete a database in cpanel
$db="db"; $curl = curl_init(); curl_setopt ($curl, CURLOPT_URL, "http://$cpanel_user: $cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin /sql/deldb.html?db=".$cpanel_user."_".$db); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result=curl_exec ($curl); curl_close ($curl);
Tags: Codes, PHP
Posted in Codes, PHP/MYSQL, Scripts | No Comments »
PHP xml to csv
Written by Codes Tips on May 23, 2009 – 7:48 am -To create a csv file from a xml in PHP 5.0 it’s very simple, we will just have to write some lines.
We will use the SimpleXML extension that come from PHP 5.0.
SimpleXML reads an entire xml into an object that we can iterate through his properties.
To write to the csv output file we will use fputcsv.
fputcsv formats a line as csv and writes it to the file.
Suppose we are having this xml named cars.xml:
<?xml version='1.0'?> <cars> <car> <color>blue</color> <price>2000</price> </car> <car> <color>red</color> <price>10000</price> </car> <car> <color>black</color> <price>5000</price> </car> </cars>
First we should read our xml using simplexml_load_file passing the name of the file and returns an object with all the properties and values of the csv:
$xml = simplexml_load_file($filexml);
After reading it we should iterate through all the child nodes of cars and write it to the output file using fputcsv specifying the object,delimiter and enclosure. We should first convert the object into an array in order to write it to the csv:
foreach ($xml->car as $car) fputcsv($f, get_object_vars($car),',','"');
Here is the complete source code that converts xml to csv in php 5.0:
<? $filexml='cars.xml'; if (file_exists($filexml)) { $xml = simplexml_load_file($filexml); $f = fopen('cars.csv', 'w'); foreach ($xml->car as $car) { fputcsv($f, get_object_vars($car),',','"'); } fclose($f); } ?>
Download php source code for converting xml into a csv
Tags: Codes, PHP
Posted in Codes, PHP/MYSQL, Scripts | No Comments »
PHP cache file for a period
Written by Codes Tips on April 12, 2009 – 3:25 pm -The concept to cache it’s very simple in PHP. First you should have some content on a page in order to cache it. First you should have a directory where all the cached pages are stored.
How it works?
First, we should verify if the page exists in our directory, if exists then it means that the page was saved(chached) before and we can list that page. For caching a page for a period of time we should verify if the date creation of the page cached was before the time that we want to keep the page cached, otherwise we create a fresh cached page. Here is a sample code to do this:
<?php $filetocache = "../cache/".$filename.".html"; //to cache outside root directory //time to cache the page $timecache = 3600 * 24 * 7;//// calculate a period of 1 week // Here we test if the file exists and if the cached file was created in the period we specify if (file_exists($filetocache) && (time() - $time cache < filemtime($filetocache))) { include($filetocache);//we show the page that was cached previously echo "<!-- Date cached ".date('jS F Y H:i', filemtime($filetocache))." -->n"; exit; } ob_start(); // here we start the output buffer //HERE we otput the page as html .... ?>
If it not exists we list our page and in the end we save the page to the cache directory, so next time the page is loaded we will get the content from cache directory. So if it time creation is higher then our period then we list the page and rewrite again to the cache directory. Here is the code for saving the contents output displayed on the browser:
<? ob_start(); //HERE we otput the page as html ..... $filetocache = "../dircache/".$filename.".html";//this is the page that we should cache, the filename is the name of the page $fp = fopen($filetocache, 'w'); // first open the cache file for writting fwrite($fp, ob_get_contents()); // we put the output contents that are sent to browser fclose($fp); // close the file ob_end_flush(); // for sending content to the browser ?>
Tags: Codes, PHP
Posted in Codes, PHP/MYSQL | 1 Comment »


















