比较有用的php代码片段
一 从网页中提取关键词
$meta = get_meta_tags(<span style="color: #800000;">'</span><span style="color: #800000;">http://www.emoticode.net/</span><span style="color: #800000;">'</span><span style="color: #000000;">);$keywords </span>= $meta[<span style="color: #800000;">'</span><span style="color: #800000;">keywords</span><span style="color: #800000;">'</span><span style="color: #000000;">];</span><span style="color: #008000;">//</span><span style="color: #008000;"> Split keywords</span>$keywords = explode(<span style="color: #800000;">'</span><span style="color: #800000;">,</span><span style="color: #800000;">'</span><span style="color: #000000;">, $keywords );</span><span style="color: #008000;">//</span><span style="color: #008000;"> Trim them</span>$keywords = array_map( <span style="color: #800000;">'</span><span style="color: #800000;">trim</span><span style="color: #800000;">'</span><span style="color: #000000;">, $keywords );</span><span style="color: #008000;">//</span><span style="color: #008000;"> Remove empty values</span>$keywords =<span style="color: #000000;"> array_filter( $keywords );print_r( $keywords );</span>
二 查找页面上的所有链接
<span style="color: #000000;">使用DOM,你可以在任意页面上抓取链接,示例如下。</span>
- $html = file_get_contents('http://www.example.com');$dom = new DOMDocument();@$dom->loadHTML($html);// grab all the on the page$xpath = new DOMXPath($dom);$hrefs = $xpath->evaluate("/html/body//a");for ($i = 0; $i $hrefs->length; $i++) {$href = $hrefs->item($i);$url = $href->getAttribute('href');echo $url.'
';}三 创建数据URI
数据URI可以帮助将图像嵌入到HTML/CSS/<span style="color: #000000;">JS中,从而节省HTTP请求。下面的函数可以利用$file创建数据URI。 function data_uri($file, $mime) { $contents</span>=<span style="color: #000000;">file_get_contents($file); $base64</span>=<span style="color: #000000;">base64_encode($contents); echo </span><span style="color: #800000;">"</span><span style="color: #800000;">data:$mime;base64,$base64</span><span style="color: #800000;">"</span><span style="color: #000000;">;}</span>
四 下载和保存远程图片到你的服务器
<span style="color: #000000;">当你在搭建网站时,很可能会从远程服务器上下载图片保存到你自己的服务器上,下面的代码就可以帮助你实现这个功能。$image </span>= file_get_contents(<span style="color: #800000;">'</span><span style="color: #800000;">http://www.php100.com/image.jpg</span><span style="color: #800000;">'</span><span style="color: #000000;">);file_put_contents(</span><span style="color: #800000;">'</span><span style="color: #800000;">/images/image.jpg</span><span style="color: #800000;">'</span>, $image); <span style="color: #008000;">//</span><span style="color: #008000;">Where to save the image</span>
五 移除Microsoft Word HTML标签
<span style="color: #000000;"> 当你使用Microsoft Word时,会创建很多标签tag,比如font、span、style、class等,这些标签在Word中十分有用,但当你从Word中把文本粘贴到网页上,就会出现很多没用的标签。下面实用的函数可以帮助你清除所有的Word HTML标签。function cleanHTML($html) {</span><span style="color: #808080;">///</span> <span style="color: #808080;">///</span><span style="color: #008000;"> Removes all FONT and SPAN tags, and all Class and Style attributes.</span><span style="color: #808080;">///</span><span style="color: #008000;"> Designed to get rid of non-standard Microsoft Word HTML tags.</span><span style="color: #808080;">///</span> <span style="color: #008000;">//</span><span style="color: #008000;"> start by completely removing all unwanted tags</span><span style="color: #000000;">$html </span>= ereg_replace(<span style="color: #800000;">"</span><span style="color: #800000;">]*></span><span style="color: #800000;">"</span>,<span style="color: #800000;">""</span><span style="color: #000000;">,$html);</span><span style="color: #008000;">//</span><span style="color: #008000;"> then run another pass over the html (twice), removing unwanted attributes</span><span style="color: #000000;">$html </span>= ereg_replace(<span style="color: #800000;">"</span><span style="color: #800000;">]*)(class|lang|style|size|face)=(</span><span style="color: #800000;">"</span>[^<span style="color: #800000;">"</span><span style="color: #800000;">]*</span><span style="color: #800000;">"</span>|<span style="color: #800000;">'</span><span style="color: #800000;">[^</span><span style="color: #800000;">'</span>]*<span style="color: #800000;">'</span><span style="color: #800000;">|[^>]+)([^>]*)>","",$html);</span>$html = ereg_replace(<span style="color: #800000;">"</span><span style="color: #800000;">]*)(class|lang|style|size|face)=(</span><span style="color: #800000;">"</span>[^<span style="color: #800000;">"</span><span style="color: #800000;">]*</span><span style="color: #800000;">"</span>|<span style="color: #800000;">'</span><span style="color: #800000;">[^</span><span style="color: #800000;">'</span>]*<span style="color: #800000;">'</span><span style="color: #800000;">|[^>]+)([^>]*)>","",$html);</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $html}</span>
六 检测浏览器语言
<span style="color: #000000;">如果你的网站是多种语言的,下面的代码可以帮助你检测浏览器语言,它会返回客户端浏览器的默认语言。function get_client_language($availableLanguages, $</span><span style="color: #0000ff;">default</span>=<span style="color: #800000;">'</span><span style="color: #800000;">en</span><span style="color: #800000;">'</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span> (isset($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_ACCEPT_LANGUAGE</span><span style="color: #800000;">'</span><span style="color: #000000;">])) { $langs</span>=explode(<span style="color: #800000;">'</span><span style="color: #800000;">,</span><span style="color: #800000;">'</span>,$_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_ACCEPT_LANGUAGE</span><span style="color: #800000;">'</span><span style="color: #000000;">]); </span><span style="color: #0000ff;">foreach</span> ($langs <span style="color: #0000ff;">as</span><span style="color: #000000;"> $value){ $choice</span>=substr($value,<span style="color: #800080;">0</span>,<span style="color: #800080;">2</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(in_array($choice, $availableLanguages)){ </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $choice; } } } </span><span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">default</span><span style="color: #000000;">;}</span>
七 保存请求信息到本地
file_put_contents(<span style="color: #800000;">'</span><span style="color: #800000;">/tmp/all.log</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">mapping</span><span style="color: #800000;">'</span>.date(<span style="color: #800000;">"</span><span style="color: #800000;">m-d H:i:s</span><span style="color: #800000;">"</span>).<span style="color: #800000;">"</span><span style="color: #800000;">\n</span><span style="color: #800000;">"</span>,FILE_APPEND);
八 excel相互转换日期
<span style="color: #0000ff;">//如果去获取某个excel日期(格式为:2016-03-12),那么获取到的是数字,需要经过转换才能恢复<br>public</span> function excelTime($date, $time = <span style="color: #0000ff;">false</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(function_exists(<span style="color: #800000;">'</span><span style="color: #800000;">GregorianToJD</span><span style="color: #800000;">'</span><span style="color: #000000;">)){ </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (is_numeric( $date )) { $jd </span>= GregorianToJD( <span style="color: #800080;">1</span>, <span style="color: #800080;">1</span>, <span style="color: #800080;">1970</span><span style="color: #000000;"> ); $gregorian </span>= JDToGregorian( $jd + intval ( $date ) - <span style="color: #800080;">25569</span><span style="color: #000000;"> ); $date </span>= explode( <span style="color: #800000;">'</span><span style="color: #800000;">/</span><span style="color: #800000;">'</span><span style="color: #000000;">, $gregorian ); $date_str </span>= str_pad( $date [<span style="color: #800080;">2</span>], <span style="color: #800080;">4</span>, <span style="color: #800000;">'</span><span style="color: #800000;">0</span><span style="color: #800000;">'</span><span style="color: #000000;">, STR_PAD_LEFT ) .</span><span style="color: #800000;">"</span><span style="color: #800000;">-</span><span style="color: #800000;">"</span>. str_pad( $date [<span style="color: #800080;">0</span>], <span style="color: #800080;">2</span>, <span style="color: #800000;">'</span><span style="color: #800000;">0</span><span style="color: #800000;">'</span><span style="color: #000000;">, STR_PAD_LEFT ) .</span><span style="color: #800000;">"</span><span style="color: #800000;">-</span><span style="color: #800000;">"</span>. str_pad( $date [<span style="color: #800080;">1</span>], <span style="color: #800080;">2</span>, <span style="color: #800000;">'</span><span style="color: #800000;">0</span><span style="color: #800000;">'</span><span style="color: #000000;">, STR_PAD_LEFT ) . ($time </span>? <span style="color: #800000;">"</span><span style="color: #800000;"> 00:00:00</span><span style="color: #800000;">"</span> : <span style="color: #800000;">''</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $date_str; } }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #008000;">//</span><span style="color: #008000;"> $date=$date>25568? $date+1:25569;</span> <span style="color: #008000;">/*</span><span style="color: #008000;">There was a bug if Converting date before 1-1-1970 (tstamp 0)</span><span style="color: #008000;">*/</span><span style="color: #000000;"> $ofs</span>=(<span style="color: #800080;">70</span> * <span style="color: #800080;">365</span> + <span style="color: #800080;">17</span>+<span style="color: #800080;">2</span>) * <span style="color: #800080;">86400</span><span style="color: #000000;">; $date </span>= date(<span style="color: #800000;">"</span><span style="color: #800000;">Y-m-d</span><span style="color: #800000;">"</span>,($date * <span style="color: #800080;">86400</span>) - $ofs).($time ? <span style="color: #800000;">"</span><span style="color: #800000;"> 00:00:00</span><span style="color: #800000;">"</span> : <span style="color: #800000;">''</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $date; } }</span>
9 json与数据相互转换
<span style="color: #800080;">1</span><span style="color: #000000;"> json转换成数组$json </span>= <span style="color: #800000;">'</span><span style="color: #800000;">[{"id":"22","name":"33","descn":"44"}]</span><span style="color: #800000;">'</span>; <span style="color: #008000;">//</span><span style="color: #008000;">json格式的数组转换成 php的数组</span>$arr =<span style="color: #000000;"> (Array)json_decode($json);echo $arr[</span><span style="color: #800080;">0</span>]->id; <span style="color: #008000;">//</span><span style="color: #008000;">用对象的方式访问(这种是没有转换成数组,而是转换成对象的情况</span>
<span style="color: #800080;">2</span><span style="color: #000000;"> 数组转换成json$json_arr </span>= array(<span style="color: #800000;">'</span><span style="color: #800000;">WebName</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">11</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">WebSite</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">11</span><span style="color: #800000;">'</span><span style="color: #000000;">);$php_json </span>= json_encode($json_arr); <span style="color: #008000;">//</span><span style="color: #008000;">把php数组格式转换成 json 格式的数据</span>echo $php_json;

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment