截取字符串ASP/PHP通用双版本代码

sharembweb 38次浏览

摘要:截取字符串ASP/PHP通用双版本代码,ASP代码部分可通过ASP转PHP来进行转换。

截取字符串ASP代码

<%
'************************************************************
'作者:云祥孙 (精通ASP/VB/PHP/JS/Flash,交流合作可联系本人)
'版权:源代码公开,各种用途均可免费使用。 
'创建:2016-01-15
'联系:QQ313801120  交流群35915100(群里已有几百人)    邮箱313801120@qq.com   个人主页 sharembweb.com
'更多帮助,文档,更新 请加群(35915100)或浏览(sharembweb.com)获得
'*                                    Powered By 云祥孙 
'************************************************************


'截取字符串 更新20160114
'c=[A]abbccdd[/A]
'0=abbccdd
'1=[A]abbccdd[/A]
'3=[A]abbccdd
'4=abbccdd[/A]
Function strCut(ByVal content, ByVal startStr, ByVal endStr, ByVal cutType)
    'On Error Resume Next
    Dim s1, s1Str, s2, s3, c 
    If InStr(content, startStr) = False Or InStr(content, endStr) = False Then
        c = "" 
        Exit Function 
    End If 
    Select Case cutType
        '完善于20150923
        Case 1
            s1 = InStr(content, startStr) 
            s1Str = Mid(content, s1 + Len(startStr)) 
            s2 = s1 + InStr(s1Str, endStr) + Len(startStr) + Len(endStr) - 1 '为什么要减1

        Case Else
            s1 = InStr(content, startStr) + Len(startStr) 
            s1Str = Mid(content, s1) 
            'S2 = InStr(S1, Content, EndStr)
            s2 = s1 + InStr(s1Str, endStr) - 1 
        'call echo("s2",s2)
    End Select
    s3 = s2 - s1 
    If s3 >= 0 Then
        c = Mid(content, s1, s3) 
    Else
        c = "" 
    End If 
    If cutType = 3 Then
        c = startStr & c 
    End If 
    If cutType = 4 Then
        c = c & endStr 
    End If 
    strCut = c 
    'If Err.Number <> 0 Then Call eerr(startStr, content)
'doError Err.Description, "StrCut 截取字符串 函数出错,StartStr=" & EchoHTML(StartStr) & "<hr>EndStr=" & EchoHTML(EndStr)
End Function 
Dim c 
c = "[A]被截取字符 sharembweb.com[/A]" 

Response.Write("原字符=" & c & "<br>") 

Response.Write("0=" & strCut(c, "[A]", "[/A]", 0) & "<br>" & vbcrlf) 
Response.Write("1=" & strCut(c, "[A]", "[/A]", 1) & "<br>" & vbcrlf) 
'response.Write("2=" & strCut(c,"[A]","[/A]",2) & "<br>" & vbcrlf)
Response.Write("3=" & strCut(c, "[A]", "[/A]", 3) & "<br>" & vbcrlf) 
Response.Write("4=" & strCut(c, "[A]", "[/A]", 4) & "<br>" & vbcrlf) 

%> 

截取字符串PHP代码


<?PHP
//************************************************************
//作者:云祥孙 (精通ASP/VB/PHP/JS/Flash,交流合作可联系本人)
//版权:源代码公开,各种用途均可免费使用。
//创建:2016-01-15
//联系:QQ313801120  交流群35915100(群里已有几百人)    邮箱313801120@qq.com   个人主页 sharembweb.com
//更多帮助,文档,更新 请加群(35915100)或浏览(sharembweb.com)获得
//*                                    Powered By 云祥孙
//************************************************************


//截取字符串 更新20160114
//c=[A]abbccdd[/A]
//0=abbccdd
//1=[A]abbccdd[/A]
//3=[A]abbccdd
//4=abbccdd[/A]
function strCut( $content, $startStr, $endStr, $cutType){
    //On Error Resume Next
    $s1=''; $s1Str=''; $s2=''; $s3=''; $c ='';
    if( instr($content, $startStr) == false || instr($content, $endStr) == false ){
        $c = '' ;

    }
    switch ( $cutType ){
        //完善于20150923
        case 1;
        $s1 = instr($content, $startStr) ;
        $s1Str = mid($content, $s1 + strlen($startStr),-1) ;
        $s2 = $s1 + instr($s1Str, $endStr) + strlen($startStr) + strlen($endStr) - 1 ;//为什么要减1
        break;
        default;
        $s1 = instr($content, $startStr) + strlen($startStr) ;
        $s1Str = mid($content, $s1,-1) ;
        //S2 = InStr(S1, Content, EndStr)
        $s2 = $s1 + instr($s1Str, $endStr) - 1 ;
        //call echo("s2",s2)
    }
    $s3 = $s2 - $s1 ;
    if( $s3 >= 0 ){
        $c = mid($content, $s1, $s3) ;
    }else{
        $c = '' ;
    }
    if( $cutType == 3 ){
        $c = $startStr . $c ;
    }
    if( $cutType == 4 ){
        $c = $c . $endStr ;
    }
    $strCut = $c ;


    return @$strCut;
}
$c ='';
$c = '[A]被截取字符 sharembweb.com[/A]' ;

echo('原字符=' . $c . '<br>') ;

echo('0=' . strCut($c, '[A]', '[/A]', 0) . '<br>' . "\n") ;
echo('1=' . strCut($c, '[A]', '[/A]', 1) . '<br>' . "\n") ;
//response.Write("2=" & strCut(c,"[A]","[/A]",2) & "<br>" & vbcrlf)
echo('3=' . strCut($c, '[A]', '[/A]', 3) . '<br>' . "\n") ;
echo('4=' . strCut($c, '[A]', '[/A]', 4) . '<br>' . "\n") ;

?>



<?PHP
//系统函数列表
function InStr($content,$search){
	if( $search!=""){
		if(strstr($content,$search)){
			return strpos($content,$search)+1;
		}else{
			return 0;
		}
	}else{
		return 0;
	}
}
function Mid($content,$nStart,$nLength=-1){
	$nStart--;				//可以从1开始
	if($nLength==-1){
		$nLength=strlen ( $content );
	}else{
		//$nLength--;
	} 
	return substr($content, $nStart, $nLength) ;
}

?>

本文版权归作者所有,转摘请注明作者和出处!
作者:云祥孙
首发:http://sharembweb.com

随机内容

表情

共2条评论
  • 网友评论:

    赞一个

    2020-07-24 16:26:24 回复

  • 网友评论:

    学习了

    2020-07-24 16:26:24 回复

友情链接