Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "regex"

Case-insensitive URLs/routes

This works for symfony 1.2 and might be backwards compatible with 1.1 and 1.0, but I haven't tested it with those versions.

Place this file in an appropriate directory (eg. apps/myapp/lib/routing):

sfRegexExtendedRoute.class.php:

<?php
 
class sfRegexExtendedRoute extends sfRoute
{
  protected function compile()
  {
    $return = parent::compile();
 
    // Case-insensitive matching (default symfony behaviour = NOT case-insensitive).
    if (isset($this->options['case_sensitive']) && !$this->options['case_sensitive'])
    {
      // Make sure $this->regex is in the form "#...#[???]"
      // and that [???] does not already include "i" (the case-insensitive flag).
      if (($parts = explode('#', strrev($this->regex)))
          && 3 <= ($num_parts = count($parts))
          && '' == $parts[$num_parts-1]
          && false === strpos($parts[0], 'i'))
      {
        $this->regex .= 'i';
      }
    }
 
    return $return;
  }
}
 

Add the option case_sensitive: false in the route you want to make case-insensitive and set its class to sfRegexExtendedRoute:

apps/myapp/config/routing.yml:

...
my_route:
  url:      /my/case/insensitive/url/:whatever
  options:  { case_sensitive: false }
  class:    sfRegexExtendedRoute
...
 

Clear your cache:

> php symfony cc
 

Your url matching for that route should now be case-insensitive.

Changing case_sensitive: false to case_sensitive: true in routing.yml will revert the matching for that route back to (symfony default) case-sensitive matching.

by Nickolas Daskalou on 2009-05-24, tagged caseinsensitive  casesensistive  expression  regex  regular  route  routes  routing 

Phone Number Helper

format a phone number with 7, 10, or 11 digits. also can convert phone number letters to numbers

lib/helpers/PhoneHelper.php

<?php 
 
function format_phone($phone = '', $convert = false, $trim = true)
{
    // If we have not entered a phone number just return empty
    if (empty($phone)) {
        return '';
    }
 
    // Strip out any extra characters that we do not need only keep letters and numbers
    $phone = preg_replace("/[^0-9A-Za-z]/", "", $phone);
 
    // Do we want to convert phone numbers with letters to their number equivalent?
    // Samples are: 1-800-TERMINIX, 1-800-FLOWERS, 1-800-Petmeds
    if ($convert == true) {
        $replace = array('2'=>array('a','b','c'),
                 '3'=>array('d','e','f'),
                     '4'=>array('g','h','i'),
                 '5'=>array('j','k','l'),
                                 '6'=>array('m','n','o'),
                 '7'=>array('p','q','r','s'),
                 '8'=>array('t','u','v'),                                '9'=>array('w','x','y','z'));
 
        // Replace each letter with a number
        // Notice this is case insensitive with the str_ireplace instead of str_replace 
        foreach($replace as $digit=>$letters) {
            $phone = str_ireplace($letters, $digit, $phone);
        }
    }
 
    // If we have a number longer than 11 digits cut the string down to only 11
    // This is also only ran if we want to limit only to 11 characters
    if ($trim == true && strlen($phone)>11) {
        $phone = substr($phone, 0, 11);
    }                        
 
    // Perform phone number formatting here
    if (strlen($phone) == 7) {
        return preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "$1-$2", $phone);
    } elseif (strlen($phone) == 10) {
        return preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "($1) $2-$3", $phone);
    } elseif (strlen($phone) == 11) {
        return preg_replace("/([0-9a-zA-Z]{1})([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "$1($2) $3-$4", $phone);
    }
 
    // Return original phone if not 7, 10 or 11 digits long
    return $phone;
}
 
by Alex Zogheb on 2008-02-08, tagged formatting  helper  javascript  number  phone  regex 
(1 comment)