Archive

Archive for the ‘PHP’ Category

PHP may harm your computer.

October 24th, 2013 1 comment

At time of writing, all results from the PHP website are blacklisted for containing 4 trojans apparently.

php.net---Google-Search

Categories: PHP
Tags: ,

Simple PHP one-liner to generate random Lorem Ipsum (Lipsum) text.

August 16th, 2011 4 comments

Copying and pasting lipsum text from a site such as lipsum.com can be boring and tedious at times, especially when you would like to test multiple blocks and how well they will look with text of varying length.

Getting random lipsum of varying length is pretty easy in PHP, in fact you can do it on just one line of code:

$lipsum = simplexml_load_file(‘http://www.lipsum.com/feed/xml?amount=1&what=paras&start=0’)->lipsum;

Need more than one random string? Turn it into a method:

function random_lipsum($amount = 1, $what = ‘paras’, $start = 0) {

return simplexml_load_file(“http://www.lipsum.com/feed/xml?amount=$amount&what=$what&start=$start”)->lipsum;

}

Where:

  • $amount is… how much of $what you want.
  • $what is either paras, words, bytes or lists.
  • $start is whether or not to start the result with ‘Lorem ipsum dolor sit amet…
Categories: All, PHP, Web

Themes in CakePHP 2.0.0

May 23rd, 2011 5 comments

I’ve spent the best part of half a day trying to figure out why themes were not working for me in CakePHP 2.0.0

The closest I got to a solution can be found over on stackoverflow.com.

Difference is that you need to set $this->viewClass to ‘Theme’ instead of $this->view, but there are 2 more unmentioned issues with the sample code provided as a solution:

public function beforeRender() {
	if ($this->RequestHandler->isMobile()) {
		$this->viewClass = 'Theme';
		$this->theme = 'mobile';
	}
}

The first is that RequestHandler has been replaced by request.

So you may use:

if ($this->request->isMobile()) {

or

if ($this->request->is('mobile')) {

The other is that despite you’ve named your theme ‘mobile’, CakePHP’s new CamelCasing behaviours will turn this into ‘Mobile’. Because of this, you need your theme’s files to be located in ‘app/View/Themed/Mobile‘, and not in ‘app/View/Themed/mobile‘.

Categories: All, PHP, Web

Masking fixed length strings in PHP.

May 17th, 2011 1 comment

A particular problem came up in the office today, masking of credit card numbers. As my friend Leonid has pointed out, one would think that this is a very simple task for any programmer. Or not.

Having read Leonid’s solution, and finding a comment on php.net which would also mask everything but the first and last four digits of a credit card number, I still felt that I needed to throw something of my own together. So here it is:

<?php
/**
 * Mask part of a string
 *
 * <code>
 * echo maskString('4012888888881881', 6, 4, '*');
 * </code>
 *
 * @param	string	$s		String to process
 * @param	integer	$start	Number of characters to leave at start of string
 * @param	integer	$end	Number of characters to leave at end of string
 * @param	string	$char	Character to mask string with
 * @return	string
 */
function maskString($s, $start, $end, $char = 'X') {
	$middle = '';
	for ($i = 0; $i < strlen($s) - $start - $end; $i++) {
		$middle .= $char;
	}
	return preg_replace('/^(d{' . $start . '})(d+.)(d{' . $end . '})$/', '${1}' . $middle . '${3}', $s);
}
?>