Category: php


split — 用正则表达式将字符串分割到数组中

split

(PHP 3, PHP 4, PHP 5)
split — 用正则表达式将字符串分割到数组中

说明

array split ( string pattern, string string [, int limit] )

提示:

preg_split() 函数使用了

Perl 兼容正则表达式语法,通常是比

split() 更快的替代方案。如果不需要正则表达式的威力,则使用

explode() 更快,这样就不会招致正则表达式引擎的浪费。

本函数返回一个字符串数组,每个单元为

string 经区分大小写的正则表达式

pattern 作为边界分割出的子串。如果设定了

limit,则返回的数组最多包含

limit 个单元,而其中最后一个单元包含了

string 中剩余的所有部分。如果出错,则

split() 返回 FALSE。

将 /etc/passwd 中的前四个字段分割出来:

例子 1. split() 例子

如果字符串中有 n 个与

pattern 匹配的项目,则返回的数组将包含

n+1 个单元。例如,如果没有找到

pattern,则会返回一个只有一个单元的数组。当然,如果

string 为空也是这样。

解析可能用斜线,点,或横线分割的日期:

例子 2. split() 例子

// 分隔符可以是斜线,点,或横线

$date = “04/30/1973″;

list($month, $day, $year) = split (‘[/.-]‘, $date);

echo “Month: $month; Day: $day; Year: $year
\n”;

?>

想仿效 Perl 中类似的 @chars =

split(”, $str) 行为,请参考

preg_split() 函数中的例子。

注意 pattern

是一个正则表达式。如果想要用的分割字符是正则表达式中的特殊字符,要先将其转义。如果觉得

split()(或其它任何 regex 函数)行为古怪的话,请阅读包含在

PHP 发行包中 regex/ 子目录下的

regex.7 文件。该文件是手册页面格式,可以用类似

man /usr/local/src/regex/regex.7 的命令来阅读。

参见 preg_split(),spliti(),explode(),implode(),chunk_split()

和 wordwrap()。

add a note

User Contributed Notes

split

dan dot jones at lunarfish dot co dot uk

17-Aug-2006 08:24

Here’s a function to split a string into csv values where they are optionally enclosed by ” to allow values with commas in.

I think it works. Let me know if I’m wrong.

Cheers. Dan

function getCSVValues($string) {

// split the string at double quotes ”

$bits = split(‘”‘,$string);

$elements = array();

for ($i=0;$i

/*

odd numbered elements would have been

enclosed by double quotes

even numbered elements would not have been

*/

if (($i%2) == 1) {

/* if the element number is odd add the

whole string to the output array */

$elements[] = $bits[$i];

} else {

/* otherwise split the unquoted stuff at commas

and add the elements to the array */

$rest = $bits[$i];

$rest = preg_replace(“/^,/”,”",$rest);

$rest = preg_replace(“/,$/”,”",$rest);

$elements = array_merge($elements,split(‘,’,$rest));

}

}

return $elements;

}

destes at ix dot netcom dot com

14-Feb-2006 04:15

Some corrections to robin-at-teddyb’s CSV splitting function. Recall

that the point of this is to properly implement a split() function that

handles data exported to CSV, where data containing commas gets

quote-delimited.
* Problem 1: As jh-at-junetz pointed out, the +1 in robin’s

nonquoted splitting command mistakenly adds an extra element to the

resulting array.
* Problem 2: If consecutive fields are quote-delimited, the

remaining “separator” between them only contains one delimiter and no

actual fields – so an extra element gets added to the parsed array.
* Problem 3: When double-quotes appear in a spreadsheet exported

to CSV, they get escaped by doubling them, i.e. a data field reading

“this is a test of a “special” case” gets written to CSV as, “this is a

test of a “”special”" case”. These quotes are also interpreted as

top-level delimiters and (mistakenly) add extra array elements to the

output.

I have hacked a conversion of “” to a single quote ( ‘ ), but a truly

clever preg_split for the top-level splitter (instead of the explode)

might preserve the original doubled “s without bugging up the top-level

parsing. i.e., a smarter man than I could solve the problem rather

than avoiding it by replacing the bad data.

(current) Solution:

= 1) {

//Break up the string according to the delimiter character

//Each string has extraneous delimiters around it (inc the ones we added

//above), so they need to be stripped off

$temparray = split($splitter, substr($val, $delimlen, strlen($val)-$delimlen-$delimlen ) );

while(list($iarg, $ival) = each($temparray)) {

$result[] = trim($ival);

}

}

// else, the next element needing parsing is a quoted string and the comma

// here is just a single separator and contains no data, so skip it

$instring = 1;

}

}

return $result;

}

?>

RE: gcerretini at technica dot net /UTF8

08-Feb-2006 08:26

Original problem:

=================

I’ve try using split function.

$ferro=”2�12″;

$valore=split(“[�]“,$ferro);

echo $ferro.”
“;

echo “p1-”.$valore[0].”
“;

echo “p2-”.$valore[1].”
“;

echo “p3-”.$valore[2].”
“;

$ferro=”2d12″;

$valore=split(“[d]“,$ferro);

echo $ferro.”
“;

echo “p1-”.$valore[0].”
“;

echo “p2-”.$valore[1].”
“;

echo “p3-”.$valore[2].”
“;

?>

This return:

============

2�12

p1-2

p2-

p3-12

2d12

p1-2

p2-12

p3-

I use charset UTF-8. When I use char � the split function ad an empty string between “2″ and “12″… Why?

Explanation:

============
UTF-8 charset codes some characters (like the “�”

character) into two bytes. In fact the regular expresion

“[�]” contains 4 bytes (4 non-unicode characters). To

demonstrate the real situation I wrote following example:

$ferro=”2de12″;

$valore=split(“[de]“,$ferro);

echo $ferro.”
“;

echo “p1-”.$valore[0].”
“;

echo “p2-”.$valore[1].”
“;

echo “p3-”.$valore[2].”
“;

This returns:

=============

2d12

p1-2

p2-

p3-12

gcerretini at technica dot net

28-Jan-2006 02:46

I’ve try using split function.

$ferro=”2?12″;

$valore=split(“[?]“,$ferro);

echo $ferro.”
“;

echo “p1-”.$valore[0].”
“;

echo “p2-”.$valore[1].”
“;

echo “p3-”.$valore[2].”
“;

$ferro=”2d12″;

$valore=split(“[d]“,$ferro);

echo $ferro.”
“;

echo “p1-”.$valore[0].”
“;

echo “p2-”.$valore[1].”
“;

echo “p3-”.$valore[2].”
“;

?>

This return:

2?12

p1-2

p2-

p3-12

2d12

p1-2

p2-12

p3-

I use charset UTF-8

When I use char ? the split function ad an empty string between “2″ and “12″ Why?

shimon at schoolportal dot co dot il

27-Dec-2005 04:34

// **

// * splitslash()

// *

// * this function enables to split with an escape char;

// *

// * @since 25/12/05 21:26:00

// * @author Shimon Doodkin

// *

// * @param $string

// * @param $string

// * @return Array()

// **

function splitslash($split,$str,$esc=’\\\\’)

{

$o=explode($split,$str);

$oc=count($o);

$a=array();

for($i=0;$i<$oc;$i++)

{

$o2=explode($esc.$esc,$o[$i]);

$o2c=count($o2);

if($o2[$o2c-1][strlen($o2[$o2c-1])-1]==$esc)

{

$o2[$o2c-1]=substr($o2[$o2c-1],0,-1);

if($i+1<$oc) { $o[$i+1]=join($esc.$esc,$o2).$split.$o[$i+1]; } else { //echo “error”; $a[]=join($esc,$o2); //do like ok } } else { $a[]=join($esc,$o2); } } return $a; } // example: $r=splitslash(“NA”,”mooNAmooNAma\\\\ma\\NA”); print_r($r); //output: /* Array ( [0] => moo

[1] => moo

[2] => ma\\maNA

)

*/

?>

04-Dec-2005 09:57

Be advised

$arr = split(“x”, “x” );

print_r($arr);

will output:

Array

(

[0] =>

[1] =>

)

That is it will catch 2 empty strings on each side of the delimiter.

franz at fholzinger dot com

04-Nov-2005 08:34

The example from ramkumar rajendran did not work.

$line = split(“/\n”, $input_several_lines_long);

I do not know why this does not work for me.

The following has worked for me to get a maximum of 2 array parts

separated by the first new line (independant if saved under UNIX or

WINDOWS):

$line = preg_split(‘/[\n\r]+/’,$input_several_lines_long,2);

Also empty lines are not considered here.

passtschu AT freenet DOT de

24-Sep-2005 03:09

divide a string with a template. the “template dividers” are the keys for the output array.

$string_match

foreach ($template_matches[1] as $key => $value){

$output[$value] = $string_matches[($key + 1)];

}

return $output;

}

$string1 = ‘www.something.com 66.196.91.121 – - [01/Sep/2005:04:20:39 +0200] “GET /robots.txt HTTP/1.0″ 200 49 “-”‘;

$string2= ‘%Domain% %IP% – %User% \[%Date%:%Time% %TimeZone%\] “%Method% %Request% %Protocol%” %ServerCode% %Bytes% “%Referer%”‘;

print_r (string2array ($string1, $string2));

/*

Array

(

[ServerAddress] => www.something.com

[IP] => 66.196.91.121

[User] => -

[Date] => 01/Sep/2005

[Time] => 04:20:39

[TimeZone] => +0200

[Method] => GET

[Request] => /robots.txt

[Protocol] => HTTP/1.0

[ServerCode] => 200

[Bytes] => 49

[Referer] => -

)

*/

?>

jh at junetz dot de

04-Jul-2005 12:34

robin: Nice function, saved my day. The +1 at the end of split / substr is wrong, though.

robin at teddyb dot org

30-Jun-2005 12:01

Actually, this version is better than the last I submitted. The goal

here is to be able to engage in *multiple* delimeter removal passes;

for all but the last pass, set the third value to “1″, and everything

should go well.

function quotesplit( $splitter=’,', $s, $restore_quotes=0 )

{

//First step is to split it up into the bits that are surrounded by quotes

//and the bits that aren’t. Adding the delimiter to the ends simplifies

//the logic further down

$getstrings = explode(‘”‘, $splitter.$s.$splitter);

//$instring toggles so we know if we are in a quoted string or not

$delimlen = strlen($splitter);

$instring = 0;

while (list($arg, $val) = each($getstrings))

{

if ($instring==1)

{

if( $restore_quotes )

{

//Add the whole string, untouched to the previous value in the array

$result[count($result)-1] = $result[count($result)-1].’”‘.$val.’”‘;

} else {

//Add the whole string, untouched to the array

$result[] = $val;

}

$instring = 0;

}

else

{

//Break up the string according to the delimiter character

//Each string has extraneous delimiters around it (inc the ones we added

//above), so they need to be stripped off

$temparray = split($splitter, substr($val, $delimlen, strlen($val)-$delimlen-$delimlen+1 ) );

while(list($iarg, $ival) = each($temparray))

{

$result[] = trim($ival);

}

$instring = 1;

}

}

return $result;

}

robin at teddyb dot org

30-Jun-2005 11:50

wchris’s quotesplit assumes that anything that is quoted must also be a

complete delimiter-seperated entry by itself. This version does not.

It also uses split’s argument order.

function quotesplit( $splitter=’,', $s )

{

//First step is to split it up into the bits that are surrounded by quotes

//and the bits that aren’t. Adding the delimiter to the ends simplifies

//the logic further down

$getstrings = explode(‘”‘, $splitter.$s.$splitter);

//$instring toggles so we know if we are in a quoted string or not

$delimlen = strlen($splitter);

$instring = 0;

while (list($arg, $val) = each($getstrings))

{

if ($instring==1)

{

//Add the whole string, untouched to the previous value in the array

$result[count($result)-1] = $result[count($result)-1].$val;

$instring = 0;

}

else

{

//Break up the string according to the delimiter character

//Each string has extraneous delimiters around it (inc the ones we added

//above), so they need to be stripped off

$temparray = split($splitter, substr($val, $delimlen, strlen($val)-$delimlen-$delimlen+1 ) );

while(list($iarg, $ival) = each($temparray))

{

$result[] = trim($ival);

}

$instring = 1;

}

}

return $result;

}

wchris

18-Feb-2005 07:53

moritz’s quotesplit didn’t work for me. It seemed to split on a comma

even though it was between a pair of quotes. However, this did work:

function quotesplit($s, $splitter=’,')

{

//First step is to split it up into the bits that are surrounded by

quotes and the bits that aren’t. Adding the delimiter to the ends

simplifies the logic further down

$getstrings = split(‘\”‘, $splitter.$s.$splitter);

//$instring toggles so we know if we are in a quoted string or not

$delimlen = strlen($splitter);

$instring = 0;

while (list($arg, $val) = each($getstrings))

{

if ($instring==1)

{

//Add the whole string, untouched to the result array.

$result[] = $val;

$instring = 0;

}

else

{

//Break up the string according to the delimiter character

//Each string has extraneous delimiters around it (inc the ones we added above), so they need to be stripped off

$temparray = split($splitter, substr($val, $delimlen, strlen($val)-$delimlen-$delimlen ) );

while(list($iarg, $ival) = each($temparray))

{

$result[] = trim($ival);

}

$instring = 1;

}

}

return $result;

}

ramkumar rajendran

18-Jan-2005 02:09

A correction to a earlier note

If you want to use split to check on line feeds (\n), the following won’t work:

$line = split(“\n”, $input_several_lines_long);

You really have to do this instead, notice the second slash:

$line = split(“/\n”, $input_several_lines_long);

Took me a little while to figure to do

claes at dot2me.com

04-Nov-2004 08:10

Though this is obvious, the manual is a bit incorrect when claiming

that the return will always be 1+number of time the split pattern

occures. If the split pattern is the first part of the string, the

return will still be 1. E.g.

$a = split(“zz,” “zzxsj.com”);

count($a);

=> 1.

The return of this can not in anyway be seperated from the return where the split pattern is not found.

moritz

10-Apr-2004 02:54

Often you want to split CSV-Like data, so this is the function for this :)

It splits data formatted like:

1,2,3

-> [1,2,3]

1 , 3, 4

-> [1,3,4]

one; two;three

-> ['one','two','three']

“this is a string”, “this is a string with , and ;”, ‘this is a string

with quotes like ” these’, “this is a string with escaped quotes \” and

\’.”, 3

-> ['this is a string','this is a string with , and ;','this is a

string with quotes like " these','this is a string with escaped quotes

" and '.',3]

function quotesplit($s)

{

$r = Array();

$p = 0;

$l = strlen($s);

while ($p < $l) {

while (($p < $l) && (strpos(” \r\t\n”,$s[$p]) !== false)) $p++;

if ($s[$p] == ‘”‘) {

$p++;

$q = $p;

while (($p < $l) && ($s[$p] != ‘”‘)) {

if ($s[$p] == ‘\\’) { $p+=2; continue; }

$p++;

}

$r[] = stripslashes(substr($s, $q, $p-$q));

$p++;

while (($p < $l) && (strpos(” \r\t\n”,$s[$p]) !== false)) $p++;

$p++;

} else if ($s[$p] == “‘”) {

$p++;

$q = $p;

while (($p < $l) && ($s[$p] != “‘”)) {

if ($s[$p] == ‘\\’) { $p+=2; continue; }

$p++;

}

$r[] = stripslashes(substr($s, $q, $p-$q));

$p++;

while (($p < $l) && (strpos(” \r\t\n”,$s[$p]) !== false)) $p++;

$p++;

} else {

$q = $p;

while (($p < $l) && (strpos(“,;”,$s[$p]) === false)) {

$p++;

}

$r[] = stripslashes(trim(substr($s, $q, $p-$q)));

while (($p < $l) && (strpos(” \r\t\n”,$s[$p]) !== false)) $p++;

$p++;

}

}

return $r;

}

alphibia at alphibia dot com

31-Mar-2004 10:19

I’d like to correct myself, I found that after testing my last solution

it will create 5 lines no matter what… So I added this to make sure

that it only displays 5 if there are five newlines. :-)

$MaxNewLines = 5;

$BRCount = substr_count($Message, ‘
‘);

if ($BRCount<$MaxNewLines)

$MaxNewLines=$BRCount;

else if($BRCount == 0)

$MaxNewLines=1;

$Message = str_replace(chr(13), ”
“, $Message);

$MessageArray = split(”
“, $Message, $MaxNewLines);

$Message = “”; $u=0;

do {

$Message.=$MessageArray[$u].’
‘;

$u++;

} while($u<($MaxNewLines-1));

$Message.=str_replace(”
“,” “,$MessageArray[$u]);

?>

-Tim

http://www.alphibia.com

nomail at please dot now

22-Nov-2003 01:33

If you want to use split to check on line feeds (\n), the following won’t work:

$line = split(“\n”, $input_several_lines_long);

You really have to do this instead, notice the second slash:

$line = split(“\\n”, $input_several_lines_long);

Took me a little while to figure out.

krahn at niehs dot nih dot gov

25-Oct-2003 03:14

> strange things happen with split

> this didn’t work

> $vontag $vonmonat were empty strings

> list ($vontag , $vonmonat) = split (‘.’ , $fromdate); // << bad

Split is acting exactly as it should; it splits on regular expressions.

A period is a regular expression pattern for a single character.

So, an actual period must be escaped with a backslash: ‘\.’

A period within brackets is not an any-character pattern, because it does

not make sense in that context.

Beware that regular expressions can be confusing becuase there

are a few different varieties of patterns.

dalu at uni dot de

09-Oct-2003 03:26

php4.3.0

strange things happen with split

this didn’t work

$vontag $vonmonat were empty strings

function ckdate($fromdate=”01.01″, $todate=”31.12″)

{

$nowyear = date(“Y”);

list ($vontag , $vonmonat) = split (‘.’ , $fromdate); // << bad

$vondatum = “$nowyear-$vonmonat-$vontag”;

list ($bistag , $bismonat) = split (‘.’ , $todate); // << bad

$bisdatum = “$nowyear-$bismonat-$bistag”;

$von = strtotime($vondatum);

$bis = strtotime($bisdatum);

$now = time();

if (($now <= $bis) and ($now >= $von))

{

return TRUE;

}

else

{

return FALSE;

}

}

?>

however this one worked perfectly

function ckdate($fromdate=”01.01″, $todate=”31.12″)

{

$nowyear = date(“Y”);

list ($vontag , $vonmonat) = split (‘[.]‘ , $fromdate); // << good

$vondatum = “$nowyear-$vonmonat-$vontag”;

list ($bistag , $bismonat) = split (‘[.]‘ , $todate); // << good

$bisdatum = “$nowyear-$bismonat-$bistag”;

$von = strtotime($vondatum);

$bis = strtotime($bisdatum);

$now = time();

if (($now <= $bis) and ($now >= $von))

{

return TRUE;

}

else

{

return FALSE;

}

}

?>

btw this fn checks if $now if between $fromdate and $todate

use it if you like

jeffrey at jhu dot edu

11-Jan-2003 05:51

In answer to gwyne at gmx dot net, dec 1, 2002:

For split(), when using a backslash as the delimiter, you have to *double escape* the backslash.

example:

==================================

==================================

output is:

stuff::thing:doodad:

paha at paha dot hu

22-Jul-2002 09:51

It’s evident but not mentioned in the documentation that using asterisks is more restricted than in a normal regular expression.

for exaple you cannot say:

split(“;*”,$string);

because what if there’s no “;” separator?(which is covered by this regular expression)

so you have to use at least

split(“;+”,$quotatxt);

in this situation.

fotw at gmx dot net

18-Jun-2002 03:50

Ups! It seems that neither explode nor split REALY takes a STRING but

only a single character as a string for splitting the string.

I found this problem in one of my codes when trying to split a string

using “;\n” as breaking string. The result, only “;” was thaken… the

rest of the string was ignored.

Same when I tried to substitute “\n” by any other thing. :(

not at anythingspecial dot com

17-Jun-2002 09:48

If you need to do a split on a period make sure you escape the period out..

$ext_arr = split(“\.”,”something.jpg”);

… because

$ext_arr = split(“.”,”something.jpg”); won’t work properly.

kang at elpmis dot com

13-Jun-2002 02:30

This is a good way to display a comma delimited file with two columns.

The first column is the URL’s description, the second is the actual URL.

    $fname=”relatedlinks.csv”;

    $fp=fopen($fname,”r”) or die(“Error found.”);

    $line = fgets( $fp, 1024 );

    while(!feof($fp))

    {

    list($desc,$url,$dummy) = split( “,”, $line, 3 );

    print ”

  • “;

    print “$desc“;

    print “

  • \n”;

    $line = fgets( $fp, 1024 );

    }

    fclose($fp);

    ?>

jchart at sdccu dot net

01-Jun-2002 03:56

[Ed. note: Close. The pipe *is* an operator in PHP, but

the reason this fails is because it's also an operator

in the regex syntax. The distinction here is important

since a PHP operator inside a string is just a character.]

The reason your code:

$line = “12|3|Fred”;

list ($msgid, $msgref, $msgtopic)=split(‘|’, $line);

didn’t work is because the “|” symbol is an operator in PHP. If you

want to use the pipe symbol as a delimiter you must excape it with a

back slash, “\|”. You code should look like this:

$line = “12|3|Fred”;

list ($msgid, $msgref, $msgtopic)=split(‘\|’, $line);

mcgarry at tig dot com dot au

17-May-2002 06:27

split() doesn’t like NUL

characters within the string, it treats the first one it meets as the

end of the string, so if you have data you want to split that can

contain a NUL character you’ll need to convert it into something else

first, eg:

$line=str_replace(chr(0),”,$line);

PHP过滤HTML字符串

<?php
/********************************************************************
* 原文件名:Filter1.php
* 文件说明:过滤HTML字符串
* 文件编写:xuefengal@sohu.com
* 流程说明:
* 当附合要求的参数传递进filter函数后,filter()函数首先
* 把要字串中所有要过滤的标签$tag通过preg_match_all()
* 取出来,然后循环preg_match_all的匹配数组,通过preg_split()
* 函数分割每个标签为 “左边属性” = “右边值”的形式,再从要保
* 留的属性数组中循环,将preg_split()匹配的内容对应取出,构成
* 可以替换的值,后最通过str_replcae()替换掉字串中相应的标签
* 函数列表:
* function filter(&$str,$tag,$keep_attribute)
* function match($reg,&$str,$arr)
* function show($str,$title=”,$debug = True)
* 使用示例:
* //取得搜狐新闻首页
* $str = @file_get_content(“http://news.sohu.com”);
* //过滤
* filter($str,’a',’href,target,alt’);
* filter($str,’p',’align’);
* show($str,’过滤后的内容’);
********************************************************************/

$start_time = array_sum(explode(” “,microtime()));

$str = <<< HTML
<A style=”a” target=_blank href=’http://www.a.com’ xxx=xadsfa alt=”a a a” style=”aa”>site a</A>
<A alt=’b b b’ xxx=xadsfa target=_blank href=’http://www.b.com’ style=”b” style=”bb”>site b</A>
<A xxx=xadsfa style=”c” href=’http://www.c.com’ target=_blank alt=c c c style=”cc”>site c</A>
<A style=”d” href=’http://www.d.com’ xxx=xadsfa alt=d d d target=_blank style=”dd”>site d</A>
<A target=_blank style=”e” xxx=xadsfa style=”ee” alt=e e e href=’http://www.e.com’>site e</A>

<p align=right style=”font-size:10px”>adasdfasdf</p>
<p style=”font-color:red;” align=’left’>asdfasdfasdfasdf</p>
<p align=left right center>asdfasdfasdf</p>

<font color=”red” alt=adasd adsasd>asdfadsfasdf</font>
<font align=’left’ color=red>asdfasdfadf</font>
<font align=left right color=red black>asdfasdf</font>
HTML;

//显示原字串
show($str,’Html’);

/***********************************************************************************************************************************************************************/
//过滤
filter($str,’a',’href,target,alt’);
filter($str,’p',’align’);
filter($str,’font’,'color,alt’);

//显示过滤后的内容
show($str,’Result’);

//脚本运行时间
$run_time = array_sum(explode(” “,microtime())) – $start_time;
echo(‘<center>Script Run Time: ‘.$run_time.’</center>’);

/**
* 说明:过滤HTML字串
* 参数:
* $str : 要过滤的HTML字串
* $tag : 过滤的标签类型
* $keep_attribute :
* 要保留的属性,此参数形式可为
* href
* href,target,alt
* array(‘href’,'target’,'alt’)
*/
function filter(&$str,$tag,$keep_attribute) {

//检查要保留的属性的参数传递方式
if(!is_array($keep_attribute)) {
//没有传递数组进来时判断参数是否包含,号
if(strpos($keep_attribute,’,')) {
//包含,号时,切分参数串为数组
$keep_attribute = explode(‘,’,$keep_attribute);
}else {
//纯字串,构造数组
$keep_attribute = array($keep_attribute);
}
}

echo(“·过滤[$tag]标签,保留属性:”.implode(‘,’,$keep_attribute).’<br>’);

//取得所有要处理的标记
$pattern = “/<$tag(.*)<\/$tag>/i”;
preg_match_all($pattern,$str,$out);

//循环处理每个标记
foreach($out[1] as $key => $val) {
//取得a标记中有几个=
$cnt = preg_split(‘/ *=/i’,$val);
$cnt = count($cnt) -1;

//构造匹配正则
$pattern = ”;
for($i=1; $i<=$cnt; $i++) {

$pattern .= ‘( .*=.*)’;
}
//完成正则表达式形成,如/(<a)( .*=.*)( .*=.*)(>.*<\/a>/i的样式
$pattern = “/(<$tag)$pattern(>.*<\/$tag>)/i”;

//取得保留属性
$replacement = match($pattern,$out[0][$key],$keep_attribute);

//替换
$str = str_replace($out[0][$key],$replacement,$str);
}
}

/**
* 说明:构造标签,保留要保留的属性
* 参数:$reg : pattern,preg_match的表达式
* $str : string,html字串
* $arr : array,要保留的属性
* 返回:
* 返回经保留处理后的标签,如
* <A href=’http://www.e.com’ target=_blank alt=e e e>e.com</A>
*/
function match($reg,&$str,$arr) {

//match
preg_match($reg,$str,$out);

//取出保留的属性
$keep_attribute = ”;
foreach($arr as $k1=>$v1) {
//定义的要保留的属性的数组
foreach($out as $k2=>$v2) {
//匹配=后的数组
$attribute = trim(substr($v2,0,strpos($v2,’=')));
//=前面的
if($v1 == $attribute) {
//要保留的属性和匹配的值的=前的部分相同
$keep_attribute .= $v2;
//保存此匹配部分的值
}
}

}

//构造返回值,结构如:<a href=xxx target=xxx>aadd</a>
$keep_attribute = $out[1].$keep_attribute.($out[count($out)-1]);
//返回值
Return $keep_attribute;
}

/**
* 显示字串内容
*/
function show($str,$title=”,$debug = True) {

if($debug) {
if(is_array($str)) {
$str = print_r($str,True);
}
$txtRows = count(explode(“\n”,$str))+1;
echo($title.’:<br><TEXTAREA NAME=”txt” ROWS=”‘.$txtRows.’” COLS=”130″>’.$str.’</TEXTAREA><br><br>’);
}

}

?>

在PHP中过滤匿名代理与透明代理的方法

为了保护网站的资源,我们通常会不允许代理访问我们的网站,以下方法可以过滤掉所有匿名代理与透明代理,但不能过滤超级代理,同时他们过滤掉局域网的代理,所以如果要正常使用,可能还要做些必要的修改!
$proxy = 0;
if($_SERVER['HTTP_VIA'] != “”)
{ $proxy = 1; }
if($_SERVER['HTTP_X_FORWARDED_FOR'] != “”)
{ $proxy = 1; }
if($_SERVER['VIA'] != “”)
{ $proxy = 1; }
if($_SERVER['HTTP_FORWARDED'] != “”)
{ $proxy = 1; }
if($_SERVER['FORWARDED'] != “”)
{ $proxy = 1; }
if($_SERVER['HTTP_X_BLUECOAT_VIA'] != “”)
{ $proxy = 1; }
if($_SERVER['HTTP_PROXY____'] != “”)
{ $proxy = 1; }
if($_SERVER['HTTP_PROXY___________'] != “”)
{ $proxy = 1; }
if($_SERVER['HTTP_PROXY_CONNECTION'] != “”)
{ $proxy = 1; }
if($_SERVER['HTTP_X_HOST'] != “”)
{ $proxy = 1; }
if($_SERVER['HTTP_X_REFERER'] != “”)
{ $proxy = 1; }
if($_SERVER['HTTP_X_SERVER_HOSTNAME'] != “”)
{ $proxy = 1; }
if($_SERVER['PROXY_HOST'] != “”)
{ $proxy = 1; }
if($_SERVER['PROXY_PORT'] != “”)
{ $proxy = 1; }
if($_SERVER['PROXY_REQUEST'] != “”)
{ $proxy = 1; }
if($_SERVER['HTTP_CLIENT_IP'] != “”)
{ $proxy = 1; }
if($_SERVER['HTTP_PRAGMA'] != “”)
{ $proxy = 1; }

在PHP中如何用正则表达式验证IP地址

方法一:

IP地址,可能都比较熟悉吧,就是由0-255间的数字,并由 . 隔开组成的。定义非常简单,而用正则表达式检验,同样也非常简单。

下面,我们来看看正则表达式是如何表示的:
/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/
这就是表示IP地址的正则表达式,别吓跑了。下面我们来详细说一下他是如何表示的。首先,我要说明的是开始和结尾的“/”是定界符,不代表什么,而 \A 和 \Z 分别代表开始和结尾,接下来,我们把中间的部分拆成两段,((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3},这是第一段,我们用不同的颜色标出了圆括号的对应位置。最后的{3}表示最红括号里的内容出现3次。而蓝色括号里的就是表示每一位数字的表示。\. 表示 . 号本身,也就是前三个数字后有点号。现在我们分析最关键的地方,如何表示0-255这些数字。即蓝色括号里的内容。

这些数字可能是一位,两位或三位,我们用符号(|)来表示,当一位或两位时,请看第一个([0-9]?[0-9]) ,问号表示出现0次或1次,这就代表了0-99之间的数字。当数字为三位数时,我们又分三种情况,1-199:我们表示为(1[0-9]{2}),200-249:我们表示为(2[0-4][0-9]),250-255:我们表示为(25[0-5])。这四种能匹配任何一种,都是0-255的范围内,整个前部分的意思就是由三个0-255的数字加小数点组成。后一部分就不用说了,就是前面数字部分的复制。只表示一个数字。现在把它整理成为一个函数。以供参考:

/**
* 检查IP地址是否正确。
*/
function checkipaddres ($ipaddres) {
$preg=”/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/”;
if(preg_match($preg,$ipaddres))return true;
return false;
}

方法二:

CODE:function IP2V(ip)
{
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正则表达式
if(re.test(ip))
{
return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1
}
else
{
throw new Error(“不是一个正确的IP地址!”)
}
}[ ]

Copyright © 2004-2010, BTHOME.COM.CN, All Rights Reserved