PHP折腾笔记

Finally have a multi-language plugin installed, which Can’t be run without a fancy switching button. I photoshopped an old school pink neon sign, with color picked from Drive =D 多语言达成!所以没有一个漂亮的切换图标怎么行?做了一个old school的粉色霓虹灯, 色彩取的 Drive 上的=D
Learned some CSS for composition. Basically have a div position=absolution covered by a div position=relative to make it floating above content while have relative position. Url is a bitch, I originally want to use cookie for switching so can get rid of ?lang=xxx. Yet the plugin(qTranslate) doesn’t offer this function. It would kill me if I want to code it myself so I have to deal with the URL parameters. I stole someone’s function for updating parameters from php url:

function addURLParameter ($url, $paramName, $paramValue) {
     $url_data = parse_url($url);
     $params = array();
     parse_str($url_data['query'], $params);
     $params[$paramName] = $paramValue;   
     $params_str = http_build_query($params);
     return http_build_url($url, array('query' => $params_str));
}

Do judge

	<?php
		$currentURL=get_option('home').$_SERVER['REQUEST_URI'] ;
		if($lang=='' || $lang=='cn') {
			$targetURL=addURLParameter($currentURL,'lang','en');
			echo "<a href=" . $targetURL ."><img src='./uploads/pics/blog_lang_en.png' height=82 w=206 /></a>";
		}
		else {
			$targetURL=addURLParameter($currentURL,'lang','cn');
			echo "<a href=" . $targetURL ."><img src='./uploads/pics/blog_lang_zh.png' height=82 w=161 /></a>";
		}
     	 ?>

And the website was down… Turned out http_build_url() is from PECLibrary not standard one, so use this instead:

 function build_url($url_data) {
     $url="";
     if(isset($url_data['host']))
     {
         $url .= $url_data['scheme'] . '://';
         if (isset($url_data['user'])) {
             $url .= $url_data['user'];
                 if (isset($url_data['pass'])) {
                     $url .= ':' . $url_data['pass'];
                 }
             $url .= '@';
         }
         $url .= $url_data['host'];
         if (isset($url_data['port'])) {
             $url .= ':' . $url_data['port'];
         }
     }
     $url .= $url_data['path'];
     if (isset($url_data['query'])) {
         $url .= '?' . $url_data['query'];
     }
     if (isset($url_data['fragment'])) {
         $url .= '#' . $url_data['fragment'];
     }
     return $url;
 }

Yuppieeee, it does look good to me. So happy to brag my language skills =P
排版看了下CSS,一个div position=absolute套在一个div position=relative里就可以浮在内容上面了。链接很伤脑筋,本来想通过cookie切换这样URL看起来漂亮点没有?lang=xxx,但是插件(qtranslate)没提供这个,我又不会php就不兴起这么可怕的自己写的念头了.. 至于php的参数,试了下手动编辑弄的想死,显然不是科学的方法,所以搜了下偷别人的函数

function addURLParameter ($url, $paramName, $paramValue) {
     $url_data = parse_url($url);
     $params = array();
     parse_str($url_data['query'], $params);
     $params[$paramName] = $paramValue;   
     $params_str = http_build_query($params);
     return http_build_url($url, array('query' => $params_str));
}

然后判断下

	<?php
		$currentURL=get_option('home').$_SERVER['REQUEST_URI'] ;
		if($lang=='' || $lang=='cn') {
			$targetURL=addURLParameter($currentURL,'lang','en');
			echo "<a href=" . $targetURL ."><img src='./uploads/pics/blog_lang_en.png' height=82 w=206 /></a>";
		}
		else {
			$targetURL=addURLParameter($currentURL,'lang','cn');
			echo "<a href=" . $targetURL ."><img src='./uploads/pics/blog_lang_zh.png' height=82 w=161 /></a>";
		}
     	 ?>

然后网站被玩坏掉了.. 一番【基本靠猜】的debug后发现http_build_url()是什么PECL包里的,就是不是标准库,于是用了下面那个(之前没注意因为不知道PECL是啥)

 function build_url($url_data) {
     $url="";
     if(isset($url_data['host']))
     {
         $url .= $url_data['scheme'] . '://';
         if (isset($url_data['user'])) {
             $url .= $url_data['user'];
                 if (isset($url_data['pass'])) {
                     $url .= ':' . $url_data['pass'];
                 }
             $url .= '@';
         }
         $url .= $url_data['host'];
         if (isset($url_data['port'])) {
             $url .= ':' . $url_data['port'];
         }
     }
     $url .= $url_data['path'];
     if (isset($url_data['query'])) {
         $url .= '?' . $url_data['query'];
     }
     if (isset($url_data['fragment'])) {
         $url .= '#' . $url_data['fragment'];
     }
     return $url;
 }

折腾了一中午达到了略骚包的效果,我很满意。

Leave a Reply

Your email address will not be published. Required fields are marked *