search
HomeBackend DevelopmentPHP Tutorial10 Practical PHP Code Snippets_PHP Tutorial
10 Practical PHP Code Snippets_PHP TutorialJul 13, 2016 pm 05:45 PM
functionphpindivualcodeauthorKey wordspracticalfragmentHighlight

作者:aolinks
关键词高亮

function highlight($sString, $aWords) {
    if (!is_array ($aWords) || emptyempty ($aWords) || !is_string ($sString)) {
        return false;
    }
 
    $sWords = implode ('|', $aWords);
    return preg_replace ('@b('.$sWords.')b@si', '$1', $sString);
}


  获取你的Feedburner的用户

function get_average_readers($feed_id,$interval = 7){
    $today = date('Y-m-d', strtotime("now"));
    $ago = date('Y-m-d', strtotime("-".$interval." days"));
    $feed_url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$feed_id."&dates=".$ago.",".$today;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $feed_url);
    $data = curl_exec($ch);
    curl_close($ch);
    $xml = new SimpleXMLElement($data);
    $fb = $xml->feed->entry['circulation'];
 
    $nb = 0;
    foreach($xml->feed->children() as $circ){
        $nb += $circ['circulation'];
    }
 
    return round($nb/$interval);
}

 

  自动生成密码

function generatePassword($length=9, $strength=0) {
    $vowels = 'aeuy';
    $consonants = 'bdghjmnpqrstvz';
    if ($strength >= 1) {
        $consonants .= 'BDGHJLMNPQRSTVWXZ';
    }
    if ($strength >= 2) {
        $vowels .= "AEUY";
    }
    if ($strength >= 4) {
        $consonants .= '23456789';
    }
    if ($strength >= 8 ) {
        $vowels .= '@#$%';
    }
 
    $password = '';
    $alt = time() % 2;
    for ($i = 0; $i         if ($alt == 1) {
            $password .= $consonants[(rand() % strlen($consonants))];
            $alt = 0;
        } else {
            $password .= $vowels[(rand() % strlen($vowels))];
            $alt = 1;
        }
    }
    return $password;
}

 

  压缩多个CSS文件

header('Content-type: text/css');
ob_start("compress");
function compress($buffer) {
  /* remove comments */
  $buffer = preg_replace('!/*[^*]**+([^/][^*]**+)*/!', '', $buffer);
  /* remove tabs, spaces, newlines, etc. */
  $buffer = str_replace(array("rn", "r", "n", "t", '  ', '    ', '    '), '', $buffer);
  return $buffer;
}
 
/* your css files */
include('master.css');
include('typography.css');
include('grid.css');
include('print.css');
include('handheld.css');
 
ob_end_flush();

 

  获取短网址

function getTinyUrl($url) {
    return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
}

 

  根据生日计算年龄

function age($date){
    $year_diff = '';
    $time = strtotime($date);
    if(FALSE === $time){
        return '';
    }
 
    $date = date('Y-m-d', $time);
    list($year,$month,$day) = explode("-",$date);
    $year_diff = date("Y") – $year;
    $month_diff = date("m") – $month;
    $day_diff = date("d") – $day;
    if ($day_diff  
    return $year_diff;
}

 

  计算执行时间

//Create a variable for start time
$time_start = microtime(true);
 
// Place your PHP/HTML/JavaScript/CSS/Etc. Here
 
//Create a variable for end time
$time_end = microtime(true);
//Subtract the two times to get seconds
$time = $time_end - $time_start;
 
echo 'Script took '.$time.' seconds to execute';

 

  PHP的维护模式

function maintenance($mode = FALSE){
    if($mode){
        if(basename($_SERVER['SCRIPT_FILENAME']) != 'maintenance.php'){
            header("Location: http://example.com/maintenance.php");
            exit;
        }
    }else{
        if(basename($_SERVER['SCRIPT_FILENAME']) == 'maintenance.php'){
            header("Location: http://example.com/");
            exit;
        }
    }
}

 

  阻止CSS样式被缓存

 

  为数字增加 stndrd 等

function make_ranked($rank) {
    $last = substr( $rank, -1 );
    $seclast = substr( $rank, -2, -1 );
    if( $last > 3 || $last == 0 ) $ext = 'th';
    else if( $last == 3 ) $ext = 'rd';
    else if( $last == 2 ) $ext = 'nd';
    else $ext = 'st'; 
 
    if( $last == 1 && $seclast == 1) $ext = 'th';
    if( $last == 2 && $seclast == 1) $ext = 'th';
    if( $last == 3 && $seclast == 1) $ext = 'th'; 
 
    return $rank.$ext;
}

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478654.htmlTechArticle作者:aolinks 关键词高亮 function highlight($sString, $aWords) { if (!is_array ($aWords) || emptyempty ($aWords) || !is_string ($sString)) { return false; } $sWords = implo...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

PyCharm新手指南:学会在PyCharm中删除项目PyCharm新手指南:学会在PyCharm中删除项目Feb 23, 2024 pm 09:39 PM

PyCharm新手指南:删除项目的实用技巧PyCharm是一款功能强大的Python集成开发环境(IDE),在进行项目开发时,有时候需要删除项目或项目中的文件。本文将介绍在PyCharm中删除项目的实用技巧,并提供具体的代码示例帮助新手更好地理解和应用。1.删除项目删除项目意味着删除整个项目文件夹,这在我们需要清理或重建项目时非常有用。在PyCharm中删

全角英文字母转换为半角形式的实用技巧全角英文字母转换为半角形式的实用技巧Mar 26, 2024 am 09:54 AM

全角英文字母转换为半角形式的实用技巧在现代生活中,我们经常会接触到英文字母,在使用电脑、手机等设备时也经常需要输入英文字母。然而,有时候我们会遇到全角英文字母的情况,而我们需要使用的是半角形式。那么,如何将全角英文字母转换为半角形式呢?下面就为大家介绍一些实用的技巧。首先,全角英文字母和数字是指在输入法中占据一个全角位置的字符,而半角英文字母和数字则是占据一

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools