Sign up to create your own snipts, or login.

Public snipts » Tram's snipts The latest snipts from Tram.

showing 1-4 of 4 snipts
  • Strip a string
    <?php
      public static function stripText($text)
      {
        // rewrite critical characters
        // French
        $text = str_replace(array('À', 'Â', 'à', 'â'), 'a', $text);
        $text = str_replace(array('É', 'È', 'Ê', 'Ë', 'é', 'è', 'ê', 'ë'), 'e', $text);
        $text = str_replace(array('Î', 'Ï', 'î', 'ï'), 'i', $text);
        $text = str_replace(array('Ô', 'ô'), 'o', $text);
        $text = str_replace(array('Ù', 'Û', 'ù', 'û'), 'u', $text);
        $text = str_replace(array('Ç', 'ç'), 'c', $text);
        // German
        $text = str_replace(array('Ä', 'ä'), 'ae', $text);
        $text = str_replace(array('Ö', 'ö'), 'oe', $text);
        $text = str_replace(array('Ü', 'ü'), 'ue', $text);
        $text = str_replace('ß', 'ss', $text);
        // Spanish
        $text = str_replace(array('Ñ', 'ñ'), 'n', $text);
        $text = str_replace(array('Á', 'á'), 'a', $text);
        $text = str_replace(array('Í', 'í'), 'i', $text);
        $text = str_replace(array('Ó', 'ó'), 'o', $text);
        $text = str_replace(array('Ú', 'ú'), 'u', $text);
        // Polish
        $text = str_replace(array('?', '?'), 'a', $text);
        $text = str_replace(array('?', '?'), 'c', $text);
        $text = str_replace(array('?', '?'), 'e', $text);
        $text = str_replace(array('?', '?'), 'l', $text);
        $text = str_replace(array('?', '?'), 'n', $text);
        $text = str_replace(array('?', '?'), 's', $text);
        $text = str_replace(array('Ó', 'ó'), 'o', $text);
        $text = str_replace(array('?', '?', '?', '?'), 'z', $text);
        
        // strip all non word chars
        $text = preg_replace('/[^a-z0-9_-]/i', ' ', $text);
    
        // strtolower is not utf8-safe, therefore it can only be done after special characters replacement
        $text = strtolower($text);
    
        // replace all white space sections with a dash
        $text = preg_replace('/\ +/', '-', $text);
    
        // trim dashes
        $text = preg_replace('/\-$/', '', $text);
        $text = preg_replace('/^\-/', '', $text);
    
        return $text;
      }
    

    copy | embed

    0 comments - tagged in  posted by Tram on Jan 16, 2009 at 5:20 a.m. EST
  • Symfony - Action for text image generation (using GD)
    <?php
      public function executeGenerateText (sfWebRequest $request) {
        $font = '/images/ROTISSAS.TTF';
        $bbox = imageftbbox(20, 0, $font, $this->getRequestParameter('text') );
        
        $width = $bbox[2] - $bbox[0] + 10;
        $height = $bbox[1] - $bbox[7] + 10;
        
        $im = imagecreatetruecolor($width, $height);
        $black = imagecolorallocate($im, 0, 0, 0);
        $white = imagecolorallocate($im, 255, 255, 255);
        
        // Set the background to be white
        // Path to our font file
        // First we create our bounding box
        
        
        imagefilledrectangle($im, 0, 0, $width, $height, $black);
    
        // This is our cordinates for X and Y
        $x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2);
        //$y = $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) + 3 ;
        $y = 28;
        
        imagefttext($im, 20, 0, 0, $y, $white, $font, $this->getRequestParameter('text'));
        
        // Output to browser
        header('Content-type: image/png');
        
        imagepng($im);
        imagedestroy($im);
    
        return sfView::NONE;
      }//executeGenerateText
    ?>
    
    
    # USAGE
    
    <img src="<? echo url_for('modulename/generateText?text=My text'); ?>" alt="" title="" />
    

    copy | embed

    0 comments - tagged in  posted by Tram on Jan 15, 2009 at 10:11 a.m. EST
  • symfony 1.2 - Add a custom filter field for generated admin
    <?php
      public function configure() {
        
        $this->widgetSchema['title'] = new sfWidgetFormFilterInput();
    
        $this->validatorSchema['title'] = new sfValidatorPass(array('required' => false));
      }//configure
    
      public function getFields() {
        return array_merge(array(
            'title'=> 'Title',
          ), parent::getFields()
        );
      } //getFields
    
      protected function addTitleColumnCriteria(Criteria $criteria, $field, $values) {
    
        // $values['text'] contains the field value, use it to change $criteria object
    
        // es. $criteria->add ('column', $values['text'])
    
      }//addTitleColumnCriteria
    
    ?>
    

    copy | embed

    1 comment - tagged in  posted by Tram on Jan 14, 2009 at 12:17 p.m. EST
  • symfony 1.2 - validation for a date range, with 'to date' field not mandatory.
    <?php
    # use it into configure() method in a myForm.class.php file
    
        $this->validatorSchema->setPostValidator( new sfValidatorOr( array (
            new sfValidatorAnd( array (
              new sfValidatorSchemaCompare ('from_date', sfValidatorSchemaCompare::NOT_EQUAL, null ), 
              new sfValidatorSchemaCompare ('to_date', sfValidatorSchemaCompare::EQUAL, null )
            )) ,
            new sfValidatorSchemaCompare('from_date', sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'to_date',array('throw_global_error' => false), array('invalid' => 'The start date ("%left_field%") must be before the end date ("%right_field%")' ))
        )));
    ?>
    

    copy | embed

    1 comment - tagged in  posted by Tram on Jan 14, 2009 at 7:08 a.m. EST
Sign up to create your own snipts, or login.