Monday, May 3, 2010

Regex negation

We can use ^[\/]*$ to match all strings that contain "/".
Similarly, we can use ^[^\/]*$ to match all strings that do not contain "/".
Notice the difference- We just need to add a ^ at the starting of the pattern inside the brackets.

To check for either / or \, the regex ^[^\/|\\\\]*$ can be used. This will negate both / and \.

For more information on regex:-

Alphanumeric validation in Cake PHP not working in CentOS

Today we got a bug.
Alphanumeric checking for first and last name fields were not working in the server and in my team member's systems. But it was working without in fault in my local system. Finally our team lead found the cause for the problem and fixed it.

Any guess what the problem was..

It was because of the operating system they had. They had Centos, and mine was Ubuntu. The version of centos that they had, doesn't support alphanumeric validation.

And the fixed code is:-
var $validate = array(
'first_name' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'First Name cannot be left blank',
'last' => true ),

'alphanumeric' => array(
'rule' => array('custom', '/^[a-z0-9]*$/i'), //this is the solved portion
'message' => 'First Name must only contain letters and numbers' ),

Cake PHP Pagination - SEO friendly URL

Today I was searching for SEO friendly Cake PHP Pagination URL.
I found the following link:-

But there was not much explanation about handling sort and pagination numbers.
I tried some modifications of the code given, and finally........ I got it!

Here's it:-


Before cleaning URL:-
function index() {
$data = $this->paginate('User'); // User is the model name

$this->set('data', $data);


After cleaning URL:-
function index($page='', $sort='', $dir = '') {
if (is_numeric($page)) {
$this->passedArgs['page'] = $page;
$this->passedArgs['sort'] = $sort;

$this->passedArgs['direction'] = $dir;
$data = $this->paginate('User'); // User is the model name

$this->set('data', $data);



Before cleaning URL:-
echo $paginator->sort('First name', 'first_name', array('class'=> $paginator->sortDir())) //sort
if ($paginator->counter('%pages%') > 1):
echo $paginator->numbers();
echo $paginator->prev('previous', null, null, array('class' => 'disabled'));
echo $paginator->next('next', null, null, array('class' => 'disabled'));

After cleaning URL:-

$replace_arr = array("page:", "sort:", "direction:"); // strings to be replaced from the pagination links

echo str_replace($replace_arr, '', $paginator->sort('First Name', 'first_name', array('class'=> $paginator->sortDir()))); //sort

if ($paginator->counter('%pages%') > 1):
$prev_link = str_replace($replace_arr, '', $paginator->prev('previous', null, null, array('class' => 'disabled')));
$prev_link = preg_replace('/\/1"/', '"', $prev_link);
$next_link = str_replace($replace_arr, '', $paginator->next('next', null, null, array('class' => 'disabled')));
echo preg_replace('/\/1"/', '"', str_replace($replace_arr, '',$paginator->numbers()));
echo $prev_link;
echo $next_link;

About My Blog

Hi Viewers,

I was thinking of a blog where I can share my knowledge.

What can we loose by sharing the knowledge that we have..? Nothing at all...
After long hours of searching and thinking, when I find a solution to a problem, I think of sharing it.
Now I can't wait any more..

Since I'm a PHP programmer, I'll be focusing in that area. Anyone who is interested in PHP, is welcome.
This is my first blog. So please support..