snippet

Snippet to get the youtube id from an url in PHP

Snippet to get the youtube id from an url in PHP


public static function parseYouTube($str) {
                $youtube_id_length = 11;
               
                if (preg_match(‘#((\?|\&)v\=|/embed/|/v/|/youtu.be/|\#././.+?/)(.{’.$youtube_id_length.‘})#i’, $str, $matches))
                        return $matches[3];
                else {
                        if (mb_strpos($str,‘/user/’) !== false) {
                                return mb_substr($str,strlen($str)-11,11);
                        } else {
                                return $str;
                        }
                };
               
                /*
                Currently works for the following url formats.
                http://youtu.be/7-luJRn6u9c
                http://youtube.com/embed/7-luJRn6u9c
                http://youtube.com/v/7-luJRn6u9c
                http://youtube.com?v=7-luJRn6u9c
                http://youtube.com/watch?v=7-luJRn6u9c
                http://www.youtube.com/HuskyStarcraft#p/u/5/7-luJRn6u9c
                http://www.youtube.com/user/HuskyStarcraft#p/u/1/7-luJRn6u9c
                */
             
        }
 

Credits for Ramsez Stamper

snippet

Comments (0)

Permalink

Utilizando Swift numa task (symfony 1.3/1.4)

Ao utilizar esse código na task:

$message = Swift_Message::newInstance();

Tive o seguinte erro:

PHP Fatal error: Class ‘Swift_Message’ not found in […]

Solução:

$message = $this->getMailer()->compose("from@mail","to@mail","subject","body");

cli
php
snippet
swift

Comments (0)

Permalink

Aumentando o timeout da sessão do usuário (sf 1.2)

Editar arquivo apps/(aplicacao)/config/factories.yml e alterar os segundos do ‘timeout’:

all:
  user:
   class: myUser
   param:
     timeout:         2700
 

php
snippet
yml

Comments (0)

Permalink

Symfony – Snippet – Maneiras de acessar o objeto User

Esses dias precisei utilizar o objeto User do symfony dentro dos forms.

Pesquisando na internet achei diversas maneiras de acessar o objeto pelo symfony.

Template / View

 $user = $sf_user;

Model ou Form

 $user = sfContext::getInstance()->getUser();

Action

 $user = $this->getUser();

ou

 $user = sfContext::getInstance()->getUser();

Creio que esse último exemplo funcione em qualquer lugar do symfony.

font: http://erisds.co.uk/symfony/snippet-symfony-user-access-the-user-object

php
snippet

Comments (0)

Permalink

Snippet – Slug String


<?php
 
function slugString($string, $replacement = ‘_’)
{
  $aux = preg_quote($replacement, ‘/’);
 
  $map = array(
    ‘/à|á|ã|â/’ => ‘a’,
    ‘/è|é|ê|ẽ|ë/’ => ‘e’,
    ‘/ì|í|î/’ => ‘i’,
    ‘/ò|ó|ô|õ|ø/’ => ‘o’,
    ‘/ù|ú|ũ|û/’ => ‘u’,
    ‘/ç/’ => ‘c’,
    ‘/ñ/’ => ‘n’,
    ‘/ä|æ/’ => ‘ae’,
    ‘/ö/’ => ‘oe’,
    ‘/ü/’ => ‘ue’,
    ‘/Ä/’ => ‘Ae’,
    ‘/Ü/’ => ‘Ue’,
    ‘/Ö/’ => ‘Oe’,
    ‘/ß/’ => ‘ss’,
    ‘/[^\w\s]/’ => ‘ ‘,
    ‘/\s+/’ => $replacement
  );
 
  return preg_replace(array_keys($map), array_values($map), $string);
}
 

Fonte: http://api.cakephp.org/view_source/inflector/#line-480

php
snippet

Comments (1)

Permalink