home

Quickies, by Andrea Olivato

text

Simple PHP updating counter on a Bash shell

If you’re running a php script on a shell and want to know what it’s doing, at which point of the script it is you can simply use the chr function to print a backspace and updating a counter while running.

Below is a quick example

	
<?php
	$howmany = 20;
	echo "I have to do $howmany cycles\n";
	for($a=0;$a<$howmany;$a++) {
		$bkspacestring = "";
		for($k=0;$k<strlen(($a-1));$k++)
			$bkspacestring .= chr(8);
		echo $bkspacestring.$a;
		sleep(1);
	}
	echo "\nDid it!\n";
?>

6 months ago

July 28, 2011
Comments (View)
text

Regex to change any BR in space in #PHP

This is a simple example showing how to change any <br> contained in an HTML string into a space, avoiding double spaces.

<?php
$string = 'Lorem <br>ipsum<br style="border:1px solid #CCC" />dolor<br /> sit<br style="height:1000px;">amet';

/* Stripping all other tags
 * [optional, depends on what you want to do 
 */
$string = strip_tags($string,'<br>');

/* Changing all <br> types into spaces */
$string = preg_replace('/<br[^>]*>/',' ',$string);

/* Avoiding double spaces */
$string = preg_replace('/[\ ]+/',' ',$string);

echo $string;

/* Will output: 'Lorem ipsum dolor sit amet' */

?>

2 years ago

October 14, 2009
Comments (View)
text

Be sure that Ajax calls came from your domain in #php

Useful for be sure nobody steals your resources (cpu ecc) this script checks if the ajax call started from your domain. If not… dies.

$ref_domain = parse_url($_SERVER['HTTP_REFERER']);
if ( $_SERVER['SERVER_NAME'] != $ref_domain['host']) 
    die;

2 years ago

August 24, 2009
Comments (View)
text

Randomize an Array in #php using shuffle

Simple way to re-order an array by random in php.

<?php
    /* First of all I create an array */
    $array = Array(1,2,3,4); 

    /* Now I shuffle it */
    shuffle($array);

    /* Here's the disordered result */
    echo implode(', ',$array);
?>

2 years ago

August 17, 2009
php
Comments (View)
text

[PHP] How much memory does your app consume?

Reblogged codepuzzling:

Here it is a simple function to display how much memory your PHP script is consuming.

// debug function with time, memory consumption (MB) and optional custom message
public function getMemoryUsage($message="", $echo=1) 
{
 $mem_used = memory_get_usage(true)/ 1024 / 1024;
 $mem_peak = memory_get_peak_usage(true) / 1024 / 1024;
 preg_match("/(\d)+/",ini_get('memory_limit'),$matches);  // regexp, takes only digits
 $mem_limit = $matches[0];
 $percentual = ($mem_used * $mem_limit) / 100;

 $res = date("H:i:s",time())." ".$message." ";		
 $res .= "[MEM_USED: $mem_used MB ($percentual%) - Peak: $mem_peak MB]\n";
 if($echo) echo $res;
 else return $res;
}

2 years ago

July 8, 2009
php
reblogged via codepuzzling
Comments (View)
text

[PHP] How to retrive HTTP headers

codepuzzling:

Anyway response header returned by apache_response_headers() consists only of server signature in my case, but in firebug I can see a lot more of them.

$var = "REQUEST";
foreach (apache_request_headers() as $name => $value) {
    $var.= "$name: $value
"; } $var.= "RESPONSE"; foreach (apache_response_headers() as $name => $value) { $var.= "$name: $value
"; } echo $var;

2 years ago

June 5, 2009
reblogged via codepuzzling
Comments (View)
text

Retrieve Options like Bash in Php : getopt()

Simply retrieve and use Options passed from command line to php

#!/usr/bin/php

<?php
	/* 
		@ Options without ":" do not accept any argument
		@ Options with a single ":" require an argument. 
			No argument input will cause an error
		@ Options with a double "::" accept an argument. 
			No argument input won't cause any error
	*/
	$shortopts = "vhr:o::"; 

	/* 
		Behave of longopts is exactly the 
		same of short ones
	*/

	$longopts  = array(
		"verbose",        
		"help",   
		"required:",     
		"optional::",    
	);

	$options = getopt($shortopts, $longopts);

	var_dump($options);

?>

2 years ago

May 15, 2009
php
Comments (View)