Have you ever developed a web Project that needed an auto refreshing data all the time?
If not you will probably have to soon.
This is my new post on Woork Up! Check it out:
Showing posts with label php. Show all posts
Showing posts with label php. Show all posts
Sunday, September 26, 2010
Sunday, July 18, 2010
Formater Class with Regular Expressions
Here is a another useful class for formating any type of data, using regular expression.
It has two parts, one var called $formats where we store the REGEX and one funcion called format where we execute it.
You may use it for anything you need, all you've to do it simply add your regex to the $formats and there you go!
This is the type of class you'll need in every project you do!
It has two parts, one var called $formats where we store the REGEX and one funcion called format where we execute it.
class Formater{
public static $formats=array(
'date2sql' => array("'^([0-9]{2}).([0-9]{2}).([0-9]{4})'","$3-$2-$1"),
'sql2date' => array("'^([0-9]{4}).([0-9]{2}).([0-9]{2})'","$3/$2/$1"),
);
public static function format($string,$type) {
if (!isset(self::$formats[$type])) return $string;
$find = self::$formats[$type][0];
$replace = self::$formats[$type][1];
return preg_replace($find,$replace,$string);
}
}
// Mode of use - Como usar
$date1 = Formater::format('2010-08-01','sql2date'); // returns 01/08/2010
$date2 = Formatter::format('01/08/2010','date2sql'); // returns 2010-08-01
You may use it for anything you need, all you've to do it simply add your regex to the $formats and there you go!
This is the type of class you'll need in every project you do!
Classe Formatadora com Expressões Regulares
Aqui está outra classe muito útil para formatar qualquer tipo de dado, usando expressões regulares!
A classe consiste em duas partes, a primeira é uma variável chamada $formats onde nós guardamos as expressões regulares e uma função chamada format, onde executamos tudo.
Você pode usar para o que precisar! Tudo que você precisa fazer é adicionar sua regex na var $formats e pronto!
Este é o tipo de classe que você precisará em todo o projeto que você faça!
A classe consiste em duas partes, a primeira é uma variável chamada $formats onde nós guardamos as expressões regulares e uma função chamada format, onde executamos tudo.
class Formater{
public static $formats=array(
'date2sql' => array("'^([0-9]{2}).([0-9]{2}).([0-9]{4})'","$3-$2-$1"),
'sql2date' => array("'^([0-9]{4}).([0-9]{2}).([0-9]{2})'","$3/$2/$1"),
);
public static function format($string,$type) {
if (!isset(self::$formats[$type])) return $string;
$find = self::$formats[$type][0];
$replace = self::$formats[$type][1];
return preg_replace($find,$replace,$string);
}
}
// Mode of use - Como usar
$date1 = Formater::format('2010-08-01','sql2date'); // returns 01/08/2010
$date2 = Formatter::format('01/08/2010','date2sql'); // returns 2010-08-01
Você pode usar para o que precisar! Tudo que você precisa fazer é adicionar sua regex na var $formats e pronto!
Este é o tipo de classe que você precisará em todo o projeto que você faça!
Simple $_GET and $_POST request manager
This is a really simple but extremally useful class/function.
Esta é uma função muito simples porém extremamente útil.
Esta é uma função muito simples porém extremamente útil.
class Request{
public static function getParam($name,$default = null) {
return isset($_GET[$name]) ? $_GET[$name] : (isset($_POST[$name]) ? $_POST[$name] : $default);
}
}
//mode of use - como usar
$var = Request::getParam('name','defaultValue');
Cleaning your HTML
What I see in the internet is that people don't know exactly how to organize their programing language inside the HTML tags.
People are using this:
instead of:
Another ordinary situation is this:
and how to handle it:
Another very usefull situation is while using if
alternative:
There's another if alternative that's REALLY
usefull:
These are simple but very important situation. If your code is clean everything is easier, and that's what we want, right?
Give it a try! It's really usefull to take advantage of those alternative syntaxes:
for: endfor;
foreach: endforeach;
while: endwhile;
switch: endswitch;
if: endif;
People are using this:
$html = "
- " . $var . "
instead of:
- <?php echo $var;?>
Another ordinary situation is this:
<table>
<?php
foreach($var as $k=>$v){
echo "<tr>";
echo "<td>".$v."</td>";
echo "</td>";
}
?>
</table>
and how to handle it:
<table> <?php foreach($var as $k=>$v):?> <tr> <td><?php echo $v;?></td> </tr> <?php endforeach;?> </table>
Another very usefull situation is while using if
if($a==$b){
//lot of codes here
} else {
// more code..
}
alternative:
<?php if($a==$b):?> // code <?php endif;?> // or <?php if($a==$b):?> // codes <?php else: ?> // codes <?php endif;?>
There's another if alternative that's REALLY
usefull:
$a = $b==$c ? true : false ;
These are simple but very important situation. If your code is clean everything is easier, and that's what we want, right?
Give it a try! It's really usefull to take advantage of those alternative syntaxes:
for: endfor;
foreach: endforeach;
while: endwhile;
switch: endswitch;
if: endif;
Limpando seu HTML
Vejo na internet nque as pessoas não sabem muito bem como organizar sua linguagem de programação dentro das tags HTML.
As pessoas estão usando:
";
echo $html;
?>
ao invés de:
Outra situação comum é:
E como lidar com ela:
Outra situação muito útil é quando usamos o if
alternativa:
Há uma outra alternativa para o if que é MUITO útil:
As situações descritas acimas são bem simples mas muito importantes. Se seu código está limpo tudo fica mais fácil, e é isto que queremos, certo?
Dê uma chance! É muito bom tirar vantagens dessas sintaxes alternativas:
for: endfor;
foreach: endforeach;
while: endwhile;
switch: endswitch;
if: endif;
As pessoas estão usando:
$html = "
- " . $var . "
";
echo $html;
?>
ao invés de:
- <?php echo $var;?>
Outra situação comum é:
<table>
<?php
foreach($var as $k=>$v){
echo "<tr>";
echo "<td>".$v."</td>";
echo "</td>";
}
?>
</table>
E como lidar com ela:
<table>
<?php foreach($var as $k=>$v):?>
<tr>
<td><?php echo $v;?></td>
</tr>
<?php endforeach;?>
</table>
Outra situação muito útil é quando usamos o if
if($a==$b){
//lot of codes here
} else {
// more code..
}
alternativa:
<?php if($a==$b):?>
// code
<?php endif;?>
// or
<?php if($a==$b):?>
// codes
<?php else: ?>
// codes
<?php endif;?>
Há uma outra alternativa para o if que é MUITO útil:
$a = $b==$c ? true : false ;
As situações descritas acimas são bem simples mas muito importantes. Se seu código está limpo tudo fica mais fácil, e é isto que queremos, certo?
Dê uma chance! É muito bom tirar vantagens dessas sintaxes alternativas:
for: endfor;
foreach: endforeach;
while: endwhile;
switch: endswitch;
if: endif;
Subscribe to:
Comments (Atom)