在坛子里看到一篇比较经典的帖子,是关于字符串切割和正则表达式应用的,觉得很不错,特意拿来为以后做东西做个参考。。。
内容如下:
/*
* jia1(jia2(jia3(1+2)+3)+4)+jia4(5+6)/jia5(7+8)
* 有这样的字符串
* 我怎么才能将其分割为
* jia3(1+2)
* jia2(jia3(1+2)+3)
* jia1(jia2(jia3(1+2)+3)+4)
* jia4(5+6)
* jia5(7+8)
*/
$str = " jia1(jia2(jia3(1+2)+3)+4)+jia4(5+6)/jia5(7+8) " ;
// 方法1
$stack = array ();
for ( $i = 0 ; $i < strlen ( $str ); $i ++ )
{
if ( $str [ $i ] == ' ( ' )
{
$stack [ $i ] = ' ( ' ;
}
else if ( $str [ $i ] == ' ) ' )
{
if ( empty ( $stack ))
{
return false ;
}
else
{
// 将数组内部指针,指向最后一个单元
//$fruits = array('apple','banana','cranberry');
//print end($fruits); // cranberry
end ( $stack );
// 返回数组中当前单元的键名
$last = key ( $stack );
// echo "last=".$last."---------"."i=".$i."--------".($last-4)."------".($i-$last+5)."<br>";
echo substr ( $str , $last - 4 , $i - $last + 5 ) . " <br> " ;
array_pop ( $stack );
}
}
}
// 方法2
echo " <br> " ;
function findStr( $str , $patternStart , $patternEnd )
{
$l = strlen ( $str );
$s = '' ;
$p = array ();
while ( $i < $l )
{
$s .= $str [ $i ];
if ( preg_match ( $patternStart , $s , $m , PREG_OFFSET_CAPTURE))
{
$a [] = $m [ 0 ][ 1 ];
}
if ( $str [ $i ] == $patternEnd )
{
$k = max ( $a );
$p [] = substr ( $str , $k , $i - $k + 1 );
array_pop ( $a );
}
$i ++ ;
}
return $p ;
}
print_r (findStr( $str , ' /jiad($/i ' , ' ) ' ));
// 方法3
echo " <br> " ;
function get_max_depth( $str ){
$max_depth = 0 ; $depth_counter = 0 ;
for ( $i = 0 ; $i < strlen ( $str ); $i ++ ){
if ( $str [ $i ] === ' ( ' ){
$depth_counter ++ ;
}
if ( $str [ $i ] === ' ) ' ){
$depth_counter -- ;
}
if ( $depth_counter > $max_depth )
$max_depth = $depth_counter ;
}
return $max_depth ;
}
for ( $i = 1 ; $i <= get_max_depth( $str ); $i ++ ){
$partten = " /(jiad(){ " . $i . " }d(+d)){ " . $i . " }/ " ;
if ( preg_match_all ( " $partten " , $str , $matches )){
foreach ( $matches [ 0 ] as $match_str )
echo " $match_str------------$i<br> " ;
}
}
* jia1(jia2(jia3(1+2)+3)+4)+jia4(5+6)/jia5(7+8)
* 有这样的字符串
* 我怎么才能将其分割为
* jia3(1+2)
* jia2(jia3(1+2)+3)
* jia1(jia2(jia3(1+2)+3)+4)
* jia4(5+6)
* jia5(7+8)
*/
$str = " jia1(jia2(jia3(1+2)+3)+4)+jia4(5+6)/jia5(7+8) " ;
// 方法1
$stack = array ();
for ( $i = 0 ; $i < strlen ( $str ); $i ++ )
{
if ( $str [ $i ] == ' ( ' )
{
$stack [ $i ] = ' ( ' ;
}
else if ( $str [ $i ] == ' ) ' )
{
if ( empty ( $stack ))
{
return false ;
}
else
{
// 将数组内部指针,指向最后一个单元
//$fruits = array('apple','banana','cranberry');
//print end($fruits); // cranberry
end ( $stack );
// 返回数组中当前单元的键名
$last = key ( $stack );
// echo "last=".$last."---------"."i=".$i."--------".($last-4)."------".($i-$last+5)."<br>";
echo substr ( $str , $last - 4 , $i - $last + 5 ) . " <br> " ;
array_pop ( $stack );
}
}
}
// 方法2
echo " <br> " ;
function findStr( $str , $patternStart , $patternEnd )
{
$l = strlen ( $str );
$s = '' ;
$p = array ();
while ( $i < $l )
{
$s .= $str [ $i ];
if ( preg_match ( $patternStart , $s , $m , PREG_OFFSET_CAPTURE))
{
$a [] = $m [ 0 ][ 1 ];
}
if ( $str [ $i ] == $patternEnd )
{
$k = max ( $a );
$p [] = substr ( $str , $k , $i - $k + 1 );
array_pop ( $a );
}
$i ++ ;
}
return $p ;
}
print_r (findStr( $str , ' /jiad($/i ' , ' ) ' ));
// 方法3
echo " <br> " ;
function get_max_depth( $str ){
$max_depth = 0 ; $depth_counter = 0 ;
for ( $i = 0 ; $i < strlen ( $str ); $i ++ ){
if ( $str [ $i ] === ' ( ' ){
$depth_counter ++ ;
}
if ( $str [ $i ] === ' ) ' ){
$depth_counter -- ;
}
if ( $depth_counter > $max_depth )
$max_depth = $depth_counter ;
}
return $max_depth ;
}
for ( $i = 1 ; $i <= get_max_depth( $str ); $i ++ ){
$partten = " /(jiad(){ " . $i . " }d(+d)){ " . $i . " }/ " ;
if ( preg_match_all ( " $partten " , $str , $matches )){
foreach ( $matches [ 0 ] as $match_str )
echo " $match_str------------$i<br> " ;
}
}