代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
PHP
】
关于字符串处理的代码片段
作者:
红薯
/ 发布于
2011/10/26
/
674
<div> <h3><i>[代码]</i> 移除 HTML 标签</h3> <div id="highlighter_868613" class="syntaxhighlighter php"> <div class="bar "> <div class="toolbar"><a style="width: 16px; height: 16px" class="item viewSource" title="view source" href="http://www.oschina.net/code/snippet_12_6759#viewSource">view source</a> <div class="item copyToClipboard"><object id="highlighter_868613_clipboard" title="copy to clipboard" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" type="application/x-shockwave-flash" height="16"> <param name="_cx" value="423" /> <param name="_cy" value="423" /> <param name="FlashVars" value="highlighterId=highlighter_868613" /> <param name="Movie" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="Src" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="WMode" value="Transparent" /> <param name="Play" value="0" /> <param name="Loop" value="-1" /> <param name="Quality" value="High" /> <param name="SAlign" value="" /> <param name="Menu" value="0" /> <param name="Base" value="" /> <param name="AllowScriptAccess" value="always" /> <param name="Scale" value="ShowAll" /> <param name="DeviceFont" value="0" /> <param name="EmbedMovie" value="0" /> <param name="BGColor" value="" /> <param name="SWRemote" value="" /> <param name="MovieData" value="" /> <param name="SeamlessTabbing" value="1" /> <param name="Profile" value="0" /> <param name="ProfileAddress" value="" /> <param name="ProfilePort" value="0" /> <param name="AllowNetworking" value="all" /> <param name="AllowFullScreen" value="false" /></object></div> <a style="width: 16px; height: 16px" class="item printSource" title="print" href="http://www.oschina.net/code/snippet_12_6759#printSource">print</a><a style="width: 16px; height: 16px" class="item about" title="?" href="http://www.oschina.net/code/snippet_12_6759#about">?</a></div> </div> <div class="lines"> <div class="line alt1"> <table> <tbody> <tr> <td class="number">1</td> <td class="content">"php variable">$text "php plain">= "php functions">strip_tags"php plain">("php variable">$input"php plain">, "php string">"""php plain">);</td> </tr> </tbody> </table> </div> </div> </div> <div class="code_piece"> <h3><i>[代码]</i> 返回 $start 和 $end 之间的文本</h3> <div id="highlighter_787847" class="syntaxhighlighter php"> <div class="bar "> <div class="toolbar"><a style="width: 16px; height: 16px" class="item viewSource" title="view source" href="http://www.oschina.net/code/snippet_12_6759#viewSource">view source</a> <div class="item copyToClipboard"><object id="highlighter_787847_clipboard" title="copy to clipboard" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" type="application/x-shockwave-flash" height="16"> <param name="_cx" value="423" /> <param name="_cy" value="423" /> <param name="FlashVars" value="highlighterId=highlighter_787847" /> <param name="Movie" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="Src" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="WMode" value="Transparent" /> <param name="Play" value="0" /> <param name="Loop" value="-1" /> <param name="Quality" value="High" /> <param name="SAlign" value="" /> <param name="Menu" value="0" /> <param name="Base" value="" /> <param name="AllowScriptAccess" value="always" /> <param name="Scale" value="ShowAll" /> <param name="DeviceFont" value="0" /> <param name="EmbedMovie" value="0" /> <param name="BGColor" value="" /> <param name="SWRemote" value="" /> <param name="MovieData" value="" /> <param name="SeamlessTabbing" value="1" /> <param name="Profile" value="0" /> <param name="ProfileAddress" value="" /> <param name="ProfilePort" value="0" /> <param name="AllowNetworking" value="all" /> <param name="AllowFullScreen" value="false" /></object></div> <a style="width: 16px; height: 16px" class="item printSource" title="print" href="http://www.oschina.net/code/snippet_12_6759#printSource">print</a><a style="width: 16px; height: 16px" class="item about" title="?" href="http://www.oschina.net/code/snippet_12_6759#about">?</a></div> </div> <div class="lines"> <div class="line alt1"> <table> <tbody> <tr> <td class="number">1</td> <td class="content">"php keyword">function "php plain">GetBetween("php variable">$content"php plain">,"php variable">$start"php plain">,"php variable">$end"php plain">){</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">2</td> <td class="content">"php variable">$r "php plain">= "php functions">explode"php plain">("php variable">$start"php plain">, "php variable">$content"php plain">);</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">3</td> <td class="content">"php keyword">if "php plain">(isset("php variable">$r"php plain">[1])){</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">4</td> <td class="content">"php variable">$r "php plain">= "php functions">explode"php plain">("php variable">$end"php plain">, "php variable">$r"php plain">[1]);</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">5</td> <td class="content">"php keyword">return "php variable">$r"php plain">[0];</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">6</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">7</td> <td class="content">"php keyword">return "php string">''"php plain">;</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">8</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> </div> </div> </div> <div class="code_piece"> <h3><i>[代码]</i> 将url转换成链接</h3> <div id="highlighter_353242" class="syntaxhighlighter php"> <div class="bar "> <div class="toolbar"><a style="width: 16px; height: 16px" class="item viewSource" title="view source" href="http://www.oschina.net/code/snippet_12_6759#viewSource">view source</a> <div class="item copyToClipboard"><object id="highlighter_353242_clipboard" title="copy to clipboard" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" type="application/x-shockwave-flash" height="16"> <param name="_cx" value="423" /> <param name="_cy" value="423" /> <param name="FlashVars" value="highlighterId=highlighter_353242" /> <param name="Movie" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="Src" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="WMode" value="Transparent" /> <param name="Play" value="0" /> <param name="Loop" value="-1" /> <param name="Quality" value="High" /> <param name="SAlign" value="" /> <param name="Menu" value="0" /> <param name="Base" value="" /> <param name="AllowScriptAccess" value="always" /> <param name="Scale" value="ShowAll" /> <param name="DeviceFont" value="0" /> <param name="EmbedMovie" value="0" /> <param name="BGColor" value="" /> <param name="SWRemote" value="" /> <param name="MovieData" value="" /> <param name="SeamlessTabbing" value="1" /> <param name="Profile" value="0" /> <param name="ProfileAddress" value="" /> <param name="ProfilePort" value="0" /> <param name="AllowNetworking" value="all" /> <param name="AllowFullScreen" value="false" /></object></div> <a style="width: 16px; height: 16px" class="item printSource" title="print" href="http://www.oschina.net/code/snippet_12_6759#printSource">print</a><a style="width: 16px; height: 16px" class="item about" title="?" href="http://www.oschina.net/code/snippet_12_6759#about">?</a></div> </div> <div class="lines"> <div class="line alt1"> <table> <tbody> <tr> <td class="number">1</td> <td class="content">"php variable">$url "php plain">= "php string">"Jean-Baptiste Jung (http://www.webdevcat.com)""php plain">;</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">2</td> <td class="content">"php variable">$url "php plain">= preg_replace("php string">"#http://([A-z0-9./-]+)#""php plain">, "php string">'<a href="http://www.catswhocode.com/blog/$1" style="font-size: 12px; vertical-align: baseline; background-color: transparent; margin: 0px; padding: 0px; color: #3777af; text-decoration: none; font-weight: bold">$0</a>'"php plain">, "php variable">$url"php plain">);</td> </tr> </tbody> </table> </div> </div> </div> </div> <div class="code_piece"> <h3><i>[代码]</i> 切分字符串为140个字符</h3> <div id="highlighter_161334" class="syntaxhighlighter php"> <div class="bar "> <div class="toolbar"><a style="width: 16px; height: 16px" class="item viewSource" title="view source" href="http://www.oschina.net/code/snippet_12_6759#viewSource">view source</a> <div class="item copyToClipboard"><object id="highlighter_161334_clipboard" title="copy to clipboard" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" type="application/x-shockwave-flash" height="16"> <param name="_cx" value="423" /> <param name="_cy" value="423" /> <param name="FlashVars" value="highlighterId=highlighter_161334" /> <param name="Movie" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="Src" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="WMode" value="Transparent" /> <param name="Play" value="0" /> <param name="Loop" value="-1" /> <param name="Quality" value="High" /> <param name="SAlign" value="" /> <param name="Menu" value="0" /> <param name="Base" value="" /> <param name="AllowScriptAccess" value="always" /> <param name="Scale" value="ShowAll" /> <param name="DeviceFont" value="0" /> <param name="EmbedMovie" value="0" /> <param name="BGColor" value="" /> <param name="SWRemote" value="" /> <param name="MovieData" value="" /> <param name="SeamlessTabbing" value="1" /> <param name="Profile" value="0" /> <param name="ProfileAddress" value="" /> <param name="ProfilePort" value="0" /> <param name="AllowNetworking" value="all" /> <param name="AllowFullScreen" value="false" /></object></div> <a style="width: 16px; height: 16px" class="item printSource" title="print" href="http://www.oschina.net/code/snippet_12_6759#printSource">print</a><a style="width: 16px; height: 16px" class="item about" title="?" href="http://www.oschina.net/code/snippet_12_6759#about">?</a></div> </div> <div class="lines"> <div class="line alt1"> <table> <tbody> <tr> <td class="number">01</td> <td class="content">"php keyword">function "php plain">split_to_chunks("php variable">$to"php plain">,"php variable">$text"php plain">){</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">02</td> <td class="content">"php variable">$total_length "php plain">= (140 - "php functions">strlen"php plain">("php variable">$to"php plain">));</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">03</td> <td class="content">"php variable">$text_arr "php plain">= "php functions">explode"php plain">("php string">" ""php plain">,"php variable">$text"php plain">);</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">04</td> <td class="content">"php variable">$i"php plain">=0;</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">05</td> <td class="content">"php variable">$message"php plain">[0]="php string">"""php plain">;</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">06</td> <td class="content">"php keyword">foreach "php plain">("php variable">$text_arr "php keyword">as "php variable">$word"php plain">){</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">07</td> <td class="content">"php keyword">if "php plain">( "php functions">strlen"php plain">("php variable">$message"php plain">["php variable">$i"php plain">] . "php variable">$word "php plain">. "php string">' '"php plain">) <= "php variable">$total_length "php plain">){</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">08</td> <td class="content">"php keyword">if "php plain">("php variable">$text_arr"php plain">["php functions">count"php plain">("php variable">$text_arr"php plain">)-1] == "php variable">$word"php plain">){</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">09</td> <td class="content">"php variable">$message"php plain">["php variable">$i"php plain">] .= "php variable">$word"php plain">;</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">10</td> <td class="content">"php plain">} "php keyword">else "php plain">{</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">11</td> <td class="content">"php variable">$message"php plain">["php variable">$i"php plain">] .= "php variable">$word "php plain">. "php string">' '"php plain">;</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">12</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">13</td> <td class="content">"php plain">} "php keyword">else "php plain">{</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">14</td> <td class="content">"php variable">$i"php plain">++;</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">15</td> <td class="content">"php keyword">if "php plain">("php variable">$text_arr"php plain">["php functions">count"php plain">("php variable">$text_arr"php plain">)-1] == "php variable">$word"php plain">){</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">16</td> <td class="content">"php variable">$message"php plain">["php variable">$i"php plain">] = "php variable">$word"php plain">;</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">17</td> <td class="content">"php plain">} "php keyword">else "php plain">{</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">18</td> <td class="content">"php variable">$message"php plain">["php variable">$i"php plain">] = "php variable">$word "php plain">. "php string">' '"php plain">;</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">19</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">20</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">21</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">22</td> <td class="content">"php keyword">return "php variable">$message"php plain">;</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">23</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> </div> </div> </div> <div class="code_piece"> <h3><i>[代码]</i> 删除字符串中的URL</h3> <div id="highlighter_673882" class="syntaxhighlighter php"> <div class="bar"> <div class="toolbar"><a style="width: 16px; height: 16px" class="item viewSource" title="view source" href="http://www.oschina.net/code/snippet_12_6759#viewSource">view source</a> <div class="item copyToClipboard"><object id="highlighter_673882_clipboard" title="copy to clipboard" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" type="application/x-shockwave-flash" height="16"> <param name="_cx" value="5080" /> <param name="_cy" value="5080" /> <param name="FlashVars" value="highlighterId=highlighter_673882" /> <param name="Movie" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="Src" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="WMode" value="Transparent" /> <param name="Play" value="-1" /> <param name="Loop" value="-1" /> <param name="Quality" value="High" /> <param name="SAlign" value="" /> <param name="Menu" value="0" /> <param name="Base" value="" /> <param name="AllowScriptAccess" value="always" /> <param name="Scale" value="ShowAll" /> <param name="DeviceFont" value="0" /> <param name="EmbedMovie" value="0" /> <param name="BGColor" value="" /> <param name="SWRemote" value="" /> <param name="MovieData" value="" /> <param name="SeamlessTabbing" value="1" /> <param name="Profile" value="0" /> <param name="ProfileAddress" value="" /> <param name="ProfilePort" value="0" /> <param name="AllowNetworking" value="all" /> <param name="AllowFullScreen" value="false" /></object></div> <a style="width: 16px; height: 16px" class="item printSource" title="print" href="http://www.oschina.net/code/snippet_12_6759#printSource">print</a><a style="width: 16px; height: 16px" class="item about" title="?" href="http://www.oschina.net/code/snippet_12_6759#about">?</a></div> </div> <div class="lines"> <div class="line alt1"> <table> <tbody> <tr> <td class="number">1</td> <td class="content">"php variable">$string "php plain">= preg_replace("php string">'/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i'"php plain">, "php string">''"php plain">, "php variable">$string"php plain">);</td> </tr> </tbody> </table> </div> </div> </div> </div> <div class="code_piece"> <h3><i>[代码]</i> 将字符串转成SEO友好的字符串</h3> <div id="highlighter_275982" class="syntaxhighlighter php"> <div class="bar "> <div class="toolbar"><a style="width: 16px; height: 16px" class="item viewSource" title="view source" href="http://www.oschina.net/code/snippet_12_6759#viewSource">view source</a> <div class="item copyToClipboard"><object id="highlighter_275982_clipboard" title="copy to clipboard" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" type="application/x-shockwave-flash" height="16"> <param name="_cx" value="423" /> <param name="_cy" value="423" /> <param name="FlashVars" value="highlighterId=highlighter_275982" /> <param name="Movie" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="Src" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="WMode" value="Transparent" /> <param name="Play" value="0" /> <param name="Loop" value="-1" /> <param name="Quality" value="High" /> <param name="SAlign" value="" /> <param name="Menu" value="0" /> <param name="Base" value="" /> <param name="AllowScriptAccess" value="always" /> <param name="Scale" value="ShowAll" /> <param name="DeviceFont" value="0" /> <param name="EmbedMovie" value="0" /> <param name="BGColor" value="" /> <param name="SWRemote" value="" /> <param name="MovieData" value="" /> <param name="SeamlessTabbing" value="1" /> <param name="Profile" value="0" /> <param name="ProfileAddress" value="" /> <param name="ProfilePort" value="0" /> <param name="AllowNetworking" value="all" /> <param name="AllowFullScreen" value="false" /></object></div> <a style="width: 16px; height: 16px" class="item printSource" title="print" href="http://www.oschina.net/code/snippet_12_6759#printSource">print</a><a style="width: 16px; height: 16px" class="item about" title="?" href="http://www.oschina.net/code/snippet_12_6759#about">?</a></div> </div> <div class="lines"> <div class="line alt1"> <table> <tbody> <tr> <td class="number">1</td> <td class="content">"php keyword">function "php plain">slug("php variable">$str"php plain">){</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">2</td> <td class="content">"php variable">$str "php plain">= "php functions">strtolower"php plain">(trim("php variable">$str"php plain">));</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">3</td> <td class="content">"php variable">$str "php plain">= preg_replace("php string">'/[^a-z0-9-]/'"php plain">, "php string">'-'"php plain">, "php variable">$str"php plain">);</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">4</td> <td class="content">"php variable">$str "php plain">= preg_replace("php string">'/-+/'"php plain">, "php string">"-""php plain">, "php variable">$str"php plain">);</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">5</td> <td class="content">"php keyword">return "php variable">$str"php plain">;</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">6</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> </div> </div> </div> <div class="code_piece"> <h3><i>[代码]</i> 解析 CSV 文件</h3> <div id="highlighter_860936" class="syntaxhighlighter php"> <div class="bar "> <div class="toolbar"><a style="width: 16px; height: 16px" class="item viewSource" title="view source" href="http://www.oschina.net/code/snippet_12_6759#viewSource">view source</a> <div class="item copyToClipboard"><object id="highlighter_860936_clipboard" title="copy to clipboard" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" type="application/x-shockwave-flash" height="16"> <param name="_cx" value="423" /> <param name="_cy" value="423" /> <param name="FlashVars" value="highlighterId=highlighter_860936" /> <param name="Movie" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="Src" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="WMode" value="Transparent" /> <param name="Play" value="0" /> <param name="Loop" value="-1" /> <param name="Quality" value="High" /> <param name="SAlign" value="" /> <param name="Menu" value="0" /> <param name="Base" value="" /> <param name="AllowScriptAccess" value="always" /> <param name="Scale" value="ShowAll" /> <param name="DeviceFont" value="0" /> <param name="EmbedMovie" value="0" /> <param name="BGColor" value="" /> <param name="SWRemote" value="" /> <param name="MovieData" value="" /> <param name="SeamlessTabbing" value="1" /> <param name="Profile" value="0" /> <param name="ProfileAddress" value="" /> <param name="ProfilePort" value="0" /> <param name="AllowNetworking" value="all" /> <param name="AllowFullScreen" value="false" /></object></div> <a style="width: 16px; height: 16px" class="item printSource" title="print" href="http://www.oschina.net/code/snippet_12_6759#printSource">print</a><a style="width: 16px; height: 16px" class="item about" title="?" href="http://www.oschina.net/code/snippet_12_6759#about">?</a></div> </div> <div class="lines"> <div class="line alt1"> <table> <tbody> <tr> <td class="number">1</td> <td class="content">"php variable">$fh "php plain">= "php functions">fopen"php plain">("php string">"contacts.csv""php plain">, "php string">"r""php plain">);</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">2</td> <td class="content">"php keyword">while"php plain">("php variable">$line "php plain">= "php functions">fgetcsv"php plain">("php variable">$fh"php plain">, 1000, "php string">",""php plain">)) {</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">3</td> <td class="content">"php functions">echo "php string">"Contact: {$line[1]}""php plain">;</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">4</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> </div> </div> </div> <div class="code_piece"> <h3><i>[代码]</i> 字符串搜索</h3> <div id="highlighter_194847" class="syntaxhighlighter php"> <div class="bar"> <div class="toolbar"><a style="width: 16px; height: 16px" class="item viewSource" title="view source" href="http://www.oschina.net/code/snippet_12_6759#viewSource">view source</a> <div class="item copyToClipboard"><object id="highlighter_194847_clipboard" title="copy to clipboard" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" type="application/x-shockwave-flash" height="16"> <param name="_cx" value="5080" /> <param name="_cy" value="5080" /> <param name="FlashVars" value="highlighterId=highlighter_194847" /> <param name="Movie" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="Src" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="WMode" value="Transparent" /> <param name="Play" value="-1" /> <param name="Loop" value="-1" /> <param name="Quality" value="High" /> <param name="SAlign" value="" /> <param name="Menu" value="0" /> <param name="Base" value="" /> <param name="AllowScriptAccess" value="always" /> <param name="Scale" value="ShowAll" /> <param name="DeviceFont" value="0" /> <param name="EmbedMovie" value="0" /> <param name="BGColor" value="" /> <param name="SWRemote" value="" /> <param name="MovieData" value="" /> <param name="SeamlessTabbing" value="1" /> <param name="Profile" value="0" /> <param name="ProfileAddress" value="" /> <param name="ProfilePort" value="0" /> <param name="AllowNetworking" value="all" /> <param name="AllowFullScreen" value="false" /></object></div> <a style="width: 16px; height: 16px" class="item printSource" title="print" href="http://www.oschina.net/code/snippet_12_6759#printSource">print</a><a style="width: 16px; height: 16px" class="item about" title="?" href="http://www.oschina.net/code/snippet_12_6759#about">?</a></div> </div> <div class="lines"> <div class="line alt1"> <table> <tbody> <tr> <td class="number">1</td> <td class="content">"php keyword">function "php plain">contains("php variable">$str"php plain">, "php variable">$content"php plain">, "php variable">$ignorecase"php plain">=true){</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">2</td> <td class="content">"php keyword">if "php plain">("php variable">$ignorecase"php plain">){</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">3</td> <td class="content">"php variable">$str "php plain">= "php functions">strtolower"php plain">("php variable">$str"php plain">);</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">4</td> <td class="content">"php variable">$content "php plain">= "php functions">strtolower"php plain">("php variable">$content"php plain">);</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">5</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">6</td> <td class="content">"php keyword">return "php functions">strpos"php plain">("php variable">$content"php plain">,"php variable">$str"php plain">) ? true : false;</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">7</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> </div> </div> </div> <div class="code_piece"> <h3><i>[代码]</i> 检查字符串是否以某个串开始</h3> <div id="highlighter_703007" class="syntaxhighlighter php"> <div class="bar"> <div class="toolbar"><a style="width: 16px; height: 16px" class="item viewSource" title="view source" href="http://www.oschina.net/code/snippet_12_6759#viewSource">view source</a> <div class="item copyToClipboard"><object id="highlighter_703007_clipboard" title="copy to clipboard" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" type="application/x-shockwave-flash" height="16"> <param name="_cx" value="5080" /> <param name="_cy" value="5080" /> <param name="FlashVars" value="highlighterId=highlighter_703007" /> <param name="Movie" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="Src" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="WMode" value="Transparent" /> <param name="Play" value="-1" /> <param name="Loop" value="-1" /> <param name="Quality" value="High" /> <param name="SAlign" value="" /> <param name="Menu" value="0" /> <param name="Base" value="" /> <param name="AllowScriptAccess" value="always" /> <param name="Scale" value="ShowAll" /> <param name="DeviceFont" value="0" /> <param name="EmbedMovie" value="0" /> <param name="BGColor" value="" /> <param name="SWRemote" value="" /> <param name="MovieData" value="" /> <param name="SeamlessTabbing" value="1" /> <param name="Profile" value="0" /> <param name="ProfileAddress" value="" /> <param name="ProfilePort" value="0" /> <param name="AllowNetworking" value="all" /> <param name="AllowFullScreen" value="false" /></object></div> <a style="width: 16px; height: 16px" class="item printSource" title="print" href="http://www.oschina.net/code/snippet_12_6759#printSource">print</a><a style="width: 16px; height: 16px" class="item about" title="?" href="http://www.oschina.net/code/snippet_12_6759#about">?</a></div> </div> <div class="lines"> <div class="line alt1"> <table> <tbody> <tr> <td class="number">1</td> <td class="content">"php keyword">function "php plain">String_Begins_With("php variable">$needle"php plain">, "php variable">$haystack "php plain">{</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">2</td> <td class="content">"php keyword">return "php plain">("php functions">substr"php plain">("php variable">$haystack"php plain">, 0, "php functions">strlen"php plain">("php variable">$needle"php plain">))=="php variable">$needle"php plain">);</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">3</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> </div> </div> </div> <div class="code_piece"> <h3><i>[代码]</i> 从字符串中提取email地址</h3> <div id="highlighter_569545" class="syntaxhighlighter php"> <div class="bar"> <div class="toolbar"><a style="width: 16px; height: 16px" class="item viewSource" title="view source" href="http://www.oschina.net/code/snippet_12_6759#viewSource">view source</a> <div class="item copyToClipboard"><object id="highlighter_569545_clipboard" title="copy to clipboard" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" type="application/x-shockwave-flash" height="16"> <param name="_cx" value="5080" /> <param name="_cy" value="5080" /> <param name="FlashVars" value="highlighterId=highlighter_569545" /> <param name="Movie" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="Src" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="WMode" value="Transparent" /> <param name="Play" value="-1" /> <param name="Loop" value="-1" /> <param name="Quality" value="High" /> <param name="SAlign" value="" /> <param name="Menu" value="0" /> <param name="Base" value="" /> <param name="AllowScriptAccess" value="always" /> <param name="Scale" value="ShowAll" /> <param name="DeviceFont" value="0" /> <param name="EmbedMovie" value="0" /> <param name="BGColor" value="" /> <param name="SWRemote" value="" /> <param name="MovieData" value="" /> <param name="SeamlessTabbing" value="1" /> <param name="Profile" value="0" /> <param name="ProfileAddress" value="" /> <param name="ProfilePort" value="0" /> <param name="AllowNetworking" value="all" /> <param name="AllowFullScreen" value="false" /></object></div> <a style="width: 16px; height: 16px" class="item printSource" title="print" href="http://www.oschina.net/code/snippet_12_6759#printSource">print</a><a style="width: 16px; height: 16px" class="item about" title="?" href="http://www.oschina.net/code/snippet_12_6759#about">?</a></div> </div> <div class="lines"> <div class="line alt1"> <table> <tbody> <tr> <td class="number">01</td> <td class="content">"php keyword">function "php plain">extract_emails("php variable">$str"php plain">){</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">02</td> <td class="content">"php comments">// This regular expression extracts all emails from a string:</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">03</td> <td class="content">"php variable">$regexp "php plain">= "php string">'/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i'"php plain">;</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">04</td> <td class="content">"php plain">preg_match_all("php variable">$regexp"php plain">, "php variable">$str"php plain">, "php variable">$m"php plain">);</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">05</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">06</td> <td class="content">"php keyword">return "php plain">isset("php variable">$m"php plain">[0]) ? "php variable">$m"php plain">[0] : "php keyword">array"php plain">();</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">07</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">08</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">09</td> <td class="content">"php variable">$test_string "php plain">= 'This is a test string...</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">10</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">11</td> <td class="content">"php plain">test1@example.org</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">12</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">13</td> <td class="content">"php plain">Test different formats:</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">14</td> <td class="content">"php plain">test2@example.org;</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">15</td> <td class="content">"php plain"><a href="php string">"test3@example.org""php plain">>foobar</a></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">16</td> <td class="content">"php plain"><test4@example.org></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">17</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">18</td> <td class="content">"php plain">strange formats:</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">19</td> <td class="content">"php plain">test5@example.org</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">20</td> <td class="content">"php plain">test6[at]example.org</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">21</td> <td class="content">"php plain">test7@example.net.org.com</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">22</td> <td class="content">"php plain">test8@ example.org</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">23</td> <td class="content">"php plain">test9@!foo!.org</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">24</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">25</td> <td class="content">"php plain">foobar</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">26</td> <td class="content">"php plain">';</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">27</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">28</td> <td class="content">"php plain">print_r(extract_emails("php variable">$test_string"php plain">));</td> </tr> </tbody> </table> </div> </div> </div> </div> <div class="code_piece"> <h3><i>[代码]</i> [PHP]代码</h3> <div id="highlighter_849258" class="syntaxhighlighter php"> <div class="bar show"> <div class="toolbar"><a style="width: 16px; height: 16px" class="item viewSource" title="view source" href="http://www.oschina.net/code/snippet_12_6759#viewSource">view source</a> <div class="item copyToClipboard"><object id="highlighter_849258_clipboard" title="copy to clipboard" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" type="application/x-shockwave-flash" height="16"> <param name="_cx" value="423" /> <param name="_cy" value="423" /> <param name="FlashVars" value="highlighterId=highlighter_849258" /> <param name="Movie" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="Src" value="/js/syntax-highlighter-2.1.382/scripts/clipboard.swf" /> <param name="WMode" value="Transparent" /> <param name="Play" value="0" /> <param name="Loop" value="-1" /> <param name="Quality" value="High" /> <param name="SAlign" value="" /> <param name="Menu" value="0" /> <param name="Base" value="" /> <param name="AllowScriptAccess" value="always" /> <param name="Scale" value="ShowAll" /> <param name="DeviceFont" value="0" /> <param name="EmbedMovie" value="0" /> <param name="BGColor" value="" /> <param name="SWRemote" value="" /> <param name="MovieData" value="" /> <param name="SeamlessTabbing" value="1" /> <param name="Profile" value="0" /> <param name="ProfileAddress" value="" /> <param name="ProfilePort" value="0" /> <param name="AllowNetworking" value="all" /> <param name="AllowFullScreen" value="false" /></object></div> <a style="width: 16px; height: 16px" class="item printSource" title="print" href="http://www.oschina.net/code/snippet_12_6759#printSource">print</a><a style="width: 16px; height: 16px" class="item about" title="?" href="http://www.oschina.net/code/snippet_12_6759#about">?</a></div> </div> <div class="lines"> <div class="line alt1"> <table> <tbody> <tr> <td class="number">01</td> <td class="content">"php keyword">function "php plain">extract_emails("php variable">$str"php plain">){</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">02</td> <td class="content">"php comments">// This regular expression extracts all emails from a string:</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">03</td> <td class="content">"php variable">$regexp "php plain">= "php string">'/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i'"php plain">;</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">04</td> <td class="content">"php plain">preg_match_all("php variable">$regexp"php plain">, "php variable">$str"php plain">, "php variable">$m"php plain">);</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">05</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">06</td> <td class="content">"php keyword">return "php plain">isset("php variable">$m"php plain">[0]) ? "php variable">$m"php plain">[0] : "php keyword">array"php plain">();</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">07</td> <td class="content">"php plain">}</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">08</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">09</td> <td class="content">"php variable">$test_string "php plain">= 'This is a test string...</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">10</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">11</td> <td class="content">"php plain">test1@example.org</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">12</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">13</td> <td class="content">"php plain">Test different formats:</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">14</td> <td class="content">"php plain">test2@example.org;</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">15</td> <td class="content">"php plain"><a href="php string">"test3@example.org""php plain">>foobar</a></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">16</td> <td class="content">"php plain"><test4@example.org></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">17</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">18</td> <td class="content">"php plain">strange formats:</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">19</td> <td class="content">"php plain">test5@example.org</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">20</td> <td class="content">"php plain">test6[at]example.org</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">21</td> <td class="content">"php plain">test7@example.net.org.com</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">22</td> <td class="content">"php plain">test8@ example.org</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">23</td> <td class="content">"php plain">test9@!foo!.org</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">24</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">25</td> <td class="content">"php plain">foobar</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">26</td> <td class="content">"php plain">';</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="number">27</td> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="number">28</td> <td class="content">"php plain">print_r(extract_emails("php variable">$test_string"php plain">));</td> </tr> </tbody> </table> </div> </div> </div> </div> </div>
试试其它关键字
字符串处理
同语言下
.
用net匹配并替换iOS标准的emoji表情符号
.
处理带Emoji表情的的字符串
.
获取微信昵称时 过滤特殊字符
.
通过判断上传文件的头字符来判断文件的类型
.
模拟百度URL加密解密算法
.
以太坊检查地址是否合法
.
实现crontab解析类
.
获取每个月的开始和结束时间
.
图片上传工具类
.
APP手机应用信息采集
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
红薯
贡献的其它代码
(
14
)
.
单个 C 文件实现的 Web 服务器
.
使用 Collections.frequency 找出重复的单词
.
代码禁用 HTTP 缓存
.
一个简单的抽奖测试原型
.
使用 C# 创建一个 FTP 服务器
.
7 种将字符串反转的 Java 方法
.
猜数字游戏
.
在控制台程序中使用 CFileDialog
.
windows下批量执行sql文件
.
关于字符串处理的代码片段
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3