首页 > php开发

php反中文汉字转Unicode编码实现程序

程序

/**
 * $str 原始字符串
 * $encoding 原始字符串的编码,默认GBK
 * $prefix 编码后的前缀,默认"&#"
 * $postfix 编码后的后缀,默认";"
 */
function unicode_encode($str, $encoding = 'GBK', $prefix = '&#', $postfix = ';') {
    $str = iconv($encoding, 'UCS-2', $str);
    $arrstr = str_split($str, 2);
    $unistr = '';
    for($i = 0, $len = count($arrstr); $i < $len; $i++) {
        $dec = hexdec(bin2hex($arrstr[$i]));
        $unistr .= $prefix . $dec . $postfix;
    }
    return $unistr;
}
 
/**
 * $str Unicode编码后的字符串
 * $encoding 原始字符串的编码,默认GBK
 * $prefix 编码字符串的前缀,默认"&#"
 * $postfix 编码字符串的后缀,默认";"
 */
function unicode_decode($unistr, $encoding = 'GBK', $prefix = '&#', $postfix = ';') {
    $arruni = explode($prefix, $unistr);
    $unistr = '';
    for($i = 1, $len = count($arruni); $i < $len; $i++) {
        if (strlen($postfix) > 0) {
            $arruni[$i] = substr($arruni[$i], 0, strlen($arruni[$i]) - strlen($postfix));
        }
        $temp = intval($arruni[$i]);
        $unistr .= ($temp < 256) ? chr(0) . chr($temp) : chr($temp / 256) . chr($temp % 256);
    }
    return iconv('UCS-2', $encoding, $unistr);
}

阅读全文

php正则匹配图片路径原理与方法

提取src=里面的图片地址还不足够,因为不能保证那个地址一定是绝对地址,完全的地址,如果那是相对的呢?如果地址诸如:
albums/Candids/thumb_P1050338.jpg
/content/media/touts/5271608/5271654/15320982
那该如何是好?
有时在这些地址前面需要加http://example1.com/ ,有些甚至要加http://example1.com/example2/.../ 于是,要写出出一种法则符合所有要求,简直是天方夜谭。只能见机行事对症下药。有时,需要从前面动刀,有时需要从后面砍断。
今天,我惊讶地知道了一个道理,原来http://example.com/ 和http://example.com////// 是一样的!
http://img3.douban.com/pics/nav/lg_main_a6.png

http://img3.douban.com////pics////nav///lg_main_a6.png
最终你都能到达

阅读全文

Codigniter框架PHP POST提交到两个地址实现

Codigniter框架使用jquery+ajax/" target="_blank">jquery ajax代码如下:

 <form action="<?php echo @htmlspecialchars($url['login_url']) ?>"  method="post" enctype="application/x-www-form-urlencoded" name="form1" id="form1">
        <p><label>工资号:</label>
            <input name="Login.Token1" class="text" type="text" id="token1" />
        </p>
        <p><label>密 码:</label>
            <input name="Login.Token2" class="text" type="password" id="token2" />
        </p>
        <p>
            <input name="登录" type="submit" id="user_login" value="登录" />
            <input type="reset" value="重置" />
        </p>
        <b style="color:red"><?php echo $this->session->flashdata('error'); ?></b>
    </form>
<script>
    $(function(){
        $("#user_login").click(function(){
            var username = $("#token1").val();
            var salary_no = $("#token2").val();
            $.ajax({
                type: "POST",
                data: "username="+username+"&salary_no="+salary_no,
                url: "<?php echo site_url('http://pic4.phprm.com/2013/05/07/ajax_check_username.jpg')?>",
                dataType: "text",
                cache: false,
                error: function(){alert('error');},
                success: function(data){
                    if(data == 'yes'){
                        location.href="<?php echo site_url('http://pic4.phprm.com/2013/05/07/index.jpg')?>";
                    }else{
                        form1.submit();
                    }
                }
            });
            return false;
        });
    });
</script>

阅读全文