Archive for the ‘PHP’ Category

short PHP if statement written in one line (ternary operator)

Saturday, June 6th, 2009

Its an easy way to write short If-Statements in PHP, also known as the ternary operator.
Ternary because of the three operands: a condition, a result for true, and a result for false.

But I always need to look up this snippet, so now I write it down for reference.

$notice = ($price < 20) ? 'cheap' : 'expensive';

This code is equal to this:

if($price < 20) {
    $notice = "cheap";
}
else {
    $notice = "expensive";
}

If the variable “price” is less than 20, the variable “notice” is set to “cheap”.
Otherwise (price is equal, or greater than 20) the notice is set to “expensive”.

domain matching regex

Monday, May 11th, 2009

A regular expression that validates a given domain without its top level domain. I didnt need to validate the tld because this comes from a selectbox, so its predefined and always valid. The http protocol or the second level domain www. are both optional, it can be written or not.

$regex = "/^(?:(http:\/\/)?)(?:(w{3}\.)?)([A-Z0-9\-".utf8_encode("äÄöÖüÜ")."]{3,})$/i";
if(!preg_match($regex,$domainname)) {
	return false;
}

So the string can look like this:
http://www.liquidbass or www.liquidbass or http://liquidbass

detailed Description:

/^

This is the beginning of the regex. The ^ is used for searching all matches from the beginning of the given string.

(?:(http:\/\/)?)(?:(w{3}\.)?)

Here we have the optional protocol and the optional second level domain www. …

([A-Z0-9\-".utf8_encode("äÄöÖüÜ")."]{3,})

This part of the regex allows all characters from a to z, hyphens and all numerics from 0 to 9.
Additionally german special chars are allowed too.
All these possible characters must match a stringlength of minimum 3.

$/i

the dollar stands for the strings end, so it would be searched for matches from beginning to the end.
the /i stands for case sensivity - the given string can be in capital letters or in lowercase.

truncate text-strings without cutting words

Thursday, April 2nd, 2009

This is an easy way to truncate strings without cutting words after n characters.

bad one:
Lorem ipsum dolor sit amet, hy…

good one:
Lorem ipsum dolor sit amet, hymenaeos …

$string = "Lorem ipsum dolor sit amet, pellentesque wisi ut congue eget quam.";
$strLength = 30;
echo substr ( $string, 0, strrpos ( $string, ' ', - ( strlen ( $string ) - $strLength ) ) );

set multilingual pageTitle in a view or element

Sunday, March 22nd, 2009

I have searched for a long time until i found the reason why i couldnt make a pageTitle multilingual in a view or in an element.

First I tried to edit the title in this way and became unhappy because it was written in the content area of my page and not in the titletag.

$this->pageTitle = __('new PageTitle');

The second parameter “true” will avoid displaying the text directly.

$this->pageTitle = __('new PageTitle', true);

So, when you write it in this way, it works!

Installing Prototype/Scriptaculous into your CakePHP App

Saturday, March 21st, 2009

I wanted to install the prototype framework with the scriptaculous effects to my cakePHP Application, but after uploading the prototype files into the webroot/js folder and linking them in the layout, i got a failure message like “Undefined variable: javascript”.

This variable has to be defined in the Applications Helper, so you can put this line of code in your AppHelper class. If you dont know what to do: create a file named “app_helper.php” in your /app folder and insert these lines of code.

class AppHelper extends Helper {
	var $helpers = array('Html','Javascript','Ajax');
}

The $javascript variable is now available and you can insert your javascript files (the prototype framework in my excample) like this in the head of your layout.

		echo $javascript->link('scriptaculous/lib/prototype');
		echo $javascript->link('scriptaculous/src/scriptaculous');

		// Scripts from a view
		echo $scripts_for_layout;

apache´s access.log parsing

Tuesday, March 10th, 2009

Sure, this is just a little thing, but i liked to show the apaches long lined access.log in a better readable way.

Just opening the access.log file with PHP and parsing line per line….


$file = "/path/to/access_log";
@$fp = fopen($file,"r");

if (!$fp) {
    echo $file ."not existent!\n";
}
else {
    while (!feof($fp)) {
        $zeile = fgets($fp, 230);
        $buffer  = explode(" ", $zeile);

        $datum = substr($buffer[3], 1);        

        echo "IP-Address: ".$buffer[0]."\n";
        echo "Date: ".$datum."\n";
        echo "DeliveredPage: ".$buffer[6]."\n";
        echo "Backlink: ".$buffer[10]."\n";
        echo "User-Agent: ".$buffer[11]."\n";
        echo "

\n"; } } @fclose($fp);