2011/05/18

Multiple Preg replace in PHP

You can do multiple preg replace with one function call, for example,

PHP, using GeSHi 1.0.8.8
  1. $subject = "{hello}";
  2. $patterns = array("/{/", "/}/");
  3. $replaces = array("{ldelim}", "{rdelim}");
  4. echo preg_replace($patterns, $replaces, $subject); 

Guess what your get? you get,

{ldelim{rdelim}hello{rdelim}

The reason you get this is that perg_replace executes multiple patterns one by one, so ‘{‘ is replaced by ‘{ldelim}’, and then ‘{ldelim}’ is replaced by ‘ {ldelim{rdelim}’.

I believe this is not what you wanted to get. You only want one ‘pattern’ is replaced once. So you can do this,

PHP (brief), using GeSHi 1.0.8.8
  1. $title = "{abdcd}";
  2. $patterns = array("/{/", "/}/");
  3. $replaces = array("LC", "RC");
  4. $title = preg_replace($patterns, $replaces, $title);
  5.  
  6. $patterns = array("/LC/", "/RC/");
  7. $replaces = array("{ldelim}", "{rdelim}");
  8. echo preg_replace($patterns, $replaces, $title);

Finally, you get,

{ldelim}hello{rdelim}

Note that in smarty template, to keep brace, you have to change ‘{ ‘or ‘} ‘to ‘{ldelim}’ or ‘{rdelim}’, or add {literal} and {/literal} around the code that you don’t want smarty engine to interpret it.

2010/08/24

Insert code

<?php
//post without border to see what happen
$tool = 'Source Code Formatter';
$auther = 'Amer Gerzic';
//all syntax highlight is using inline css
?>
I did set up border, and wanted to see if I can apply css to it, then after the template change, I only need to update css file.

2010/08/17

ie hack

As an web developer, I wish there was only one browser, then we should not worry about the crossing browser compatible issue. But on the other hand, if there was only one browser, then it would be a very bad thing for the end user. So web developers are talking about ie-hack or firefox-hack, or opera-hack. But most of developers probably like firefox more than IE, who start from Firefox, and then test, tweak and fix css problem on another browser. So do I.

If don’t want to write a separate css file for IE using conditional comments, then you might use a simple character hack.

2010/07/09

Small php script in Smarty templates

Sometimes, we wanted to run a small PHP scripts inside Smarty templates. That’s good that we don’t need to touch PHP source file to add smarty variables. Here is an example,

In the PHP source code, I have smarty variable named phpvar,

<?php
$phpvar = "Hello World";
$smarty->assign('phpvar', $phpvar);
?>

And in the smarty tempate file, I wanted to split phpvar into tow variable. Sure, you can update source code, to include 2 variables, but sometime, you may do it in the template file. Here is how,

{if preg_match("/(.+)\s+(.+)/", $phpvar, $matches)}
<li>{$matches.1}</li>
<li>{$matches.2}</li>
{/if}

 

2010/06/18

http://ajax.googleapis.com is down

Many sites are hosting their javascript libraries on googleapis.com, such as jquery, which is supposed to be faster because googleapis is CDN (Content Distribution Network), however, what happen if googleapis.com is down.

Today, I see this problem, I went to nettus, slickdeals, they are extremely slow, I see this at the Firefox process bar,

connecting to ajax.googleapis.com...

Initially, I thought it is my ISP problem, I could not open “ajax.googleapis.com” at all. Then I tried http://downforeveryoneorjustme.com/, which told me, the site looks down for everyone not just for me.

I also went to Twitter to see if people are talking about this, unfortunately, their site also uses googleapis, and the search seems broken.

Anyone have the same problem?

By the time (1:55pmEST, 06-18-2010) I am writing this, the googleapis.com looks down for me for about 30 mins.

Updated: 2:00pm EST 06-18-2010, looks like the server is back. Not sure it is just my ISP problem, because ajax.googleapis.com’s home page is a not found page (BTW, I got timeout error when there was a problem), downforeveryoneorjustme.com may always say that it looks down.

2010/05/26

mysql get month from UNIX TIME

Try to count total registers in a certain month from members table, but the regdate is in UNIX TIME STAMP. Here is a solution using MYSQL's function FROM_UNIXTIME

SELECT count( * ) FROM `members` WHERE month( FROM_UNIXTIME( `regdate` ) ) =12

2010/04/09

PHP get ip function

A pretty good way to get IP, especially, your site has different servers, i.e. load balancing.

 function get_ip(){
    if (isset($_GET['ip'])) {
       $ip = stripslashes($_GET['ip']);
    } elseif (isset($_SERVER) and !empty($_SERVER)) {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else if (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
    } else {
        if (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } else if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } else {
            $ip = getenv('REMOTE_ADDR');
        }
    }
    return $ip;
}