ホームページ >バックエンド開発 >PHPチュートリアル >6 つの便利な PHP スニペットまたはヒント

6 つの便利な PHP スニペットまたはヒント

WBOY
WBOYオリジナル
2016-06-13 12:33:20940ブラウズ

六个有用的 PHP 片段或技巧

网上有很多 PHP 代码片段可以提高开发效率,也可以学习一下其中的技巧而应用在自己的项目中,下面就精选了几个比较有用的 PHP 片段。

从网页中提取关键词

从指定页面中提取关键词并显示出来。

<code class=" language-php"><span class="token variable">$meta</span> <span class="token operator">=</span> get_meta_tags<span class="token punctuation">(</span>'http<span class="token punctuation">:</span><span class="token comment">//blog.wpjam.com/');
</span><span class="token variable">$keywords</span> <span class="token operator">=</span> <span class="token variable">$meta</span><span class="token punctuation">[</span><span class="token string">'keywords'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">// 分割关键词
</span><span class="token variable">$keywords</span> <span class="token operator">=</span> <span class="token function">explode</span><span class="token punctuation">(</span><span class="token string">','</span><span class="token punctuation">,</span> <span class="token variable">$keywords</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 整理
</span><span class="token variable">$keywords</span> <span class="token operator">=</span> <span class="token function">array_map</span><span class="token punctuation">(</span> <span class="token string">'trim'</span><span class="token punctuation">,</span> <span class="token variable">$keywords</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 去掉空内容
</span><span class="token variable">$keywords</span> <span class="token operator">=</span> <span class="token function">array_filter</span><span class="token punctuation">(</span> <span class="token variable">$keywords</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>

print_r<span class="token punctuation">(</span> <span class="token variable">$keywords</span> <span class="token punctuation">)</span><span class="token punctuation">;</span></code>

得到页面中所有的链接

下面代码可以使用 PHP DOM 获取指定页面中的所有链接,仅作抛砖引玉,具体使用自由发挥。

<code class=" language-php"><span class="token variable">$html</span> <span class="token operator">=</span> <span class="token function">file_get_contents</span><span class="token punctuation">(</span>'http<span class="token punctuation">:</span><span class="token comment">//www.example.com');
</span>
<span class="token variable">$dom</span> <span class="token operator">=</span> <span class="token keyword">new</span> DOMDocument<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
@<span class="token variable">$dom</span><span class="token operator">-</span><span class="token operator">></span>loadHTML<span class="token punctuation">(</span><span class="token variable">$html</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token variable">$xpath</span> <span class="token operator">=</span> <span class="token keyword">new</span> DOMXPath<span class="token punctuation">(</span><span class="token variable">$dom</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$hrefs</span> <span class="token operator">=</span> <span class="token variable">$xpath</span><span class="token operator">-</span><span class="token operator">></span>evaluate<span class="token punctuation">(</span>"<span class="token operator">/</span>html<span class="token operator">/</span>body<span class="token comment">//a");
</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token variable">$i</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token variable">$i</span> <span class="token operator"><</span> <span class="token variable">$hrefs</span><span class="token operator">-</span><span class="token operator">></span>length<span class="token punctuation">;</span> <span class="token variable">$i</span><span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
       <span class="token variable">$href</span> <span class="token operator">=</span> <span class="token variable">$hrefs</span><span class="token operator">-</span><span class="token operator">></span>item<span class="token punctuation">(</span><span class="token variable">$i</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token variable">$href</span><span class="token operator">-</span><span class="token operator">></span>getAttribute<span class="token punctuation">(</span><span class="token string">'href'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token function">echo</span> <span class="token variable">$url</span><span class="token punctuation">.</span><span class="token string">'<br />'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code>

自动把页面中的 URL 转换成可点击的超链接

如果你发表一些文章或者做一些页面,要想放上一个超链接,必须编写一个 a 标签。使用下面这段代码可以方便的将 URL 转换成超链接输出。实现方法比较简单,大体思路就是用正则匹配出来 URL 然后处理输出超链接。

<code class=" language-php"><span class="token keyword">function</span> _make_url_clickable_cb<span class="token punctuation">(</span><span class="token variable">$matches</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token variable">$ret</span> <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span>
	<span class="token variable">$url</span> <span class="token operator">=</span> <span class="token variable">$matches</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
 
	<span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token function">empty</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span> <span class="token punctuation">)</span>
		<span class="token keyword">return</span> <span class="token variable">$matches</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token comment">// 去掉 URL 后面的标点符号
</span>	<span class="token keyword">if</span> <span class="token punctuation">(</span> in_array<span class="token punctuation">(</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string">'.'</span><span class="token punctuation">,</span> <span class="token string">','</span><span class="token punctuation">,</span> <span class="token string">';'</span><span class="token punctuation">,</span> <span class="token string">':'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span><span class="token operator">=</span> <span class="token boolean">true</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token variable">$ret</span> <span class="token operator">=</span> <span class="token function">substr</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">substr</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token variable">$matches</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token punctuation">.</span> <span class="token string">"<a href=\"</span><span class="token variable">$url</span>\<span class="token string">" rel=\"nofollow\"</span><span class="token operator">></span><span class="token variable">$url</span><span class="token operator"><</span><span class="token operator">/</span>a<span class="token operator">></span>" <span class="token punctuation">.</span> <span class="token variable">$ret</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
 
<span class="token keyword">function</span> _make_web_ftp_clickable_cb<span class="token punctuation">(</span><span class="token variable">$matches</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token variable">$ret</span> <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span>
	<span class="token variable">$dest</span> <span class="token operator">=</span> <span class="token variable">$matches</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token variable">$dest</span> <span class="token operator">=</span> 'http<span class="token punctuation">:</span><span class="token comment">//' . $dest;
</span> 
	<span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token function">empty</span><span class="token punctuation">(</span><span class="token variable">$dest</span><span class="token punctuation">)</span> <span class="token punctuation">)</span>
		<span class="token keyword">return</span> <span class="token variable">$matches</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span> in_array<span class="token punctuation">(</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token variable">$dest</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string">'.'</span><span class="token punctuation">,</span> <span class="token string">','</span><span class="token punctuation">,</span> <span class="token string">';'</span><span class="token punctuation">,</span> <span class="token string">':'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span><span class="token operator">=</span> <span class="token boolean">true</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token variable">$ret</span> <span class="token operator">=</span> <span class="token function">substr</span><span class="token punctuation">(</span><span class="token variable">$dest</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token variable">$dest</span> <span class="token operator">=</span> <span class="token function">substr</span><span class="token punctuation">(</span><span class="token variable">$dest</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$dest</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token variable">$matches</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token punctuation">.</span> <span class="token string">"<a href=\"</span><span class="token variable">$dest</span>\<span class="token string">" rel=\"nofollow\"</span><span class="token operator">></span><span class="token variable">$dest</span><span class="token operator"><</span><span class="token operator">/</span>a<span class="token operator">></span>" <span class="token punctuation">.</span> <span class="token variable">$ret</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
 
<span class="token keyword">function</span> _make_email_clickable_cb<span class="token punctuation">(</span><span class="token variable">$matches</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token variable">$email</span> <span class="token operator">=</span> <span class="token variable">$matches</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token punctuation">.</span> <span class="token string">'@'</span> <span class="token punctuation">.</span> <span class="token variable">$matches</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token variable">$matches</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token punctuation">.</span> <span class="token string">"<a href=\"</span>mailto<span class="token punctuation">:</span><span class="token variable">$email</span>\"<span class="token operator">></span><span class="token variable">$email</span><span class="token operator"><</span><span class="token operator">/</span>a<span class="token operator">></span>"<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
 
<span class="token keyword">function</span> make_clickable<span class="token punctuation">(</span><span class="token variable">$ret</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token variable">$ret</span> <span class="token operator">=</span> <span class="token string">' '</span> <span class="token punctuation">.</span> <span class="token variable">$ret</span><span class="token punctuation">;</span>
	<span class="token variable">$ret</span> <span class="token operator">=</span> preg_replace_callback<span class="token punctuation">(</span>'#<span class="token punctuation">(</span><span class="token punctuation">[</span>\s<span class="token operator">></span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">[</span>\w<span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">?</span><span class="token punctuation">:</span><span class="token comment">//[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);
</span>	<span class="token variable">$ret</span> <span class="token operator">=</span> preg_replace_callback<span class="token punctuation">(</span><span class="token string">'#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is'</span><span class="token punctuation">,</span> <span class="token string">'_make_web_ftp_clickable_cb'</span><span class="token punctuation">,</span> <span class="token variable">$ret</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token variable">$ret</span> <span class="token operator">=</span> preg_replace_callback<span class="token punctuation">(</span><span class="token string">'#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i'</span><span class="token punctuation">,</span> <span class="token string">'_make_email_clickable_cb'</span><span class="token punctuation">,</span> <span class="token variable">$ret</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
	<span class="token variable">$ret</span> <span class="token operator">=</span> preg_replace<span class="token punctuation">(</span>"#<span class="token punctuation">(</span><span class="token operator"><</span>a<span class="token punctuation">(</span> <span class="token punctuation">[</span>^<span class="token operator">></span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token deliminator">?></span><span class="token operator">|</span><span class="token operator">></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator"><</span>a <span class="token punctuation">[</span>^<span class="token operator">></span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token deliminator">?></span><span class="token punctuation">(</span><span class="token punctuation">[</span>^<span class="token operator">></span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">?</span><span class="token punctuation">)</span><span class="token operator"><</span><span class="token operator">/</span>a<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>a<span class="token operator">></span>#i<span class="token string">", "</span><span class="token variable">$1</span><span class="token variable">$3</span><span class="token operator"><</span><span class="token operator">/</span>a<span class="token operator">></span>"<span class="token punctuation">,</span> <span class="token variable">$ret</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token variable">$ret</span> <span class="token operator">=</span> trim<span class="token punctuation">(</span><span class="token variable">$ret</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token variable">$ret</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code>

用 PHP 生成 Data URI 代码

通常把图片编码成 Data URI 格式用在网页中来减少 HTTP 请求来提升前端性能。同时还有一些其他的用途。下面代码可以将文件编码成 Data URI。

<code class=" language-php"><span class="token keyword">function</span> data_uri<span class="token punctuation">(</span><span class="token variable">$file</span><span class="token punctuation">,</span> <span class="token variable">$mime</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token variable">$contents</span><span class="token operator">=</span><span class="token function">file_get_contents</span><span class="token punctuation">(</span><span class="token variable">$file</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token variable">$base64</span><span class="token operator">=</span><span class="token function">base64_encode</span><span class="token punctuation">(</span><span class="token variable">$contents</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">echo</span> "data<span class="token punctuation">:</span><span class="token variable">$mime</span><span class="token punctuation">;</span>base64<span class="token punctuation">,</span><span class="token variable">$base64</span>"<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code>

将远程图片下载到本地服务器

特别是转载文章等,为了防止对方网站关掉而导致图片丢失,通常会在发表文章的时候,将远程服务器上的图片下载到本地服务器上。下面代码简单的实现了这个需求,更多的储存位置、遍历链接还需要你自己自定义:

<code class=" language-php"><span class="token variable">$image</span> <span class="token operator">=</span> <span class="token function">file_get_contents</span><span class="token punctuation">(</span>'http<span class="token punctuation">:</span><span class="token comment">//www.url.com/image.jpg');
</span><span class="token function">file_put_contents</span><span class="token punctuation">(</span><span class="token string">'/images/image.jpg'</span><span class="token punctuation">,</span> <span class="token variable">$image</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code>

去掉文中的无用标签

当从一些文本编辑器(例如 Word)中将文本复制到网页编辑器中时,可能会有一些额外的无用标签,例如一些指定文字样式的 style 等。下面代码可以通过正则匹配来去掉这些无用标签,净化文本:

<code class=" language-php"><span class="token keyword">function</span> cleanHTML<span class="token punctuation">(</span><span class="token variable">$html</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

<span class="token comment">// 首先去掉无用的标签(可以自定义更多需要清除的标签)
</span>
<span class="token variable">$html</span> <span class="token operator">=</span> <span class="token function">ereg_replace</span><span class="token punctuation">(</span><span class="token string">"<(/)?(font|span|del|ins)[^>]*>"</span><span class="token punctuation">,</span><span class="token string">""</span><span class="token punctuation">,</span><span class="token variable">$html</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 然后再运行两遍去掉无用属性
</span>
<span class="token variable">$html</span> <span class="token operator">=</span> <span class="token function">ereg_replace</span><span class="token punctuation">(</span><span class="token string">"<([^>]*)(class|lang|style|size|face)=("</span><span class="token punctuation">[</span>^<span class="token string">"]*"</span><span class="token operator">|</span><span class="token string">'[^'</span><span class="token punctuation">]</span><span class="token operator">*</span>'<span class="token operator">|</span><span class="token punctuation">[</span>^<span class="token operator">></span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">[</span>^<span class="token operator">></span><span class="token punctuation">]</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">></span><span class="token string">","</span><span class="token operator"><</span>\<span class="token number">1</span><span class="token operator">></span>"<span class="token punctuation">,</span><span class="token variable">$html</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$html</span> <span class="token operator">=</span> <span class="token function">ereg_replace</span><span class="token punctuation">(</span><span class="token string">"<([^>]*)(class|lang|style|size|face)=("</span><span class="token punctuation">[</span>^<span class="token string">"]*"</span><span class="token operator">|</span><span class="token string">'[^'</span><span class="token punctuation">]</span><span class="token operator">*</span>'<span class="token operator">|</span><span class="token punctuation">[</span>^<span class="token operator">></span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">[</span>^<span class="token operator">></span><span class="token punctuation">]</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">></span><span class="token string">","</span><span class="token operator"><</span>\<span class="token number">1</span><span class="token operator">></span>"<span class="token punctuation">,</span><span class="token variable">$html</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">return</span> <span class="token variable">$html</span>
<span class="token punctuation">}</span></code>

?

本文固定链接: http://ylcf88.com
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。