在中间字符串数字上拆分字符串,而不会丢失结果中的数字

问题描述 投票:0回答:2

我怎样才能把这个字符串分成5个字符串。

Tampa Bay 6 Florida 5 (FINAL - OT)

生成此结果的字符串是

$results[$sport][$p]


我想将第一个数字之前的内容拆分为 $team1

将数字拆分为 $team1score

将第一个数字之后和第二个数字之前的文本拆分到 $team2

将第二个数字拆分为 $team2score

最后将括号中的内容拆分为$results


重要的是不要使用空格进行分割,因为有些团队名称有多个空格。

有什么帮助吗?看起来很复杂,但对于合适的人来说可能很容易。

其他一些有用的信息。

我正在尝试将 2 个脚本转换为 PHP。下面的两个做同样的事情,但幸运的是我没有 PHP 版本。下面是windows mobile 的。这是一个 MSCR 文件

msg = "[scores]^NL^"
did = 0
hidewaitcursor
stream = readfile(url)

foreach entry in split(stream, "&nfl", 0)
if (find(entry, "s_left") ne "0")
entry = replace(entry,"%20"," ")
split(entry, "=", 1, a, b)

if (find(entry," at ") ne "0")
if (did eq "0")
msg = msg & "^NL^^NL^[schedule]^NL^"
did = 1
endif
split(b, " at ", 1, team1, team2)
split(team2, "(", 1, team2, info)
split(info, ")", 1, info, j)
else
split(b, " ", 1, team1, b)
split(b, "(", 1, team2, info)
split(info, ")", 1, info, j)
endif

if (find(team2,"^") ne "0")
tmp = team1
team1 = team2
team2 = tmp
endif

team1 = replace(team1,"^","")
team2 = replace(team2,"^","")
info = replace(info,"FINAL","final")

msg = msg & team1 & " - " & team2 & " -- " & info & "^NL^"

这是一个 CodeFusuion 脚本,它与 MSCR 执行相同的操作

<!--- Lets do NFL --->    
<cfset espn = "http://sports.espn.go.com/nfl/bottomline/scores">
        
<cfhttp url="#espn#" method="get" resolveurl="yes" throwonerror="yes" />
<cfset myArray = arrayNew(1) />
<cfset myResult = #CFHTTP.FileContent# />

<cfif ShowNFLEspnString EQ true>
    <cfoutput>
    <br />
    ESPN NFL String URL: <a href="#espn#">#espn#</a><br />
    <br />
    #myResult#<br />
    <br />
    </cfoutput>
</cfif>

<cfset j = 1 />
<!---parse the urls, don't touch :)--->
<cfset newResult = #REReplace(myResult, "%20%20", "@", "all")# />
<cfset newResult = #REReplace(newResult, "%20", " ", "all")# />
<cfset newResult = #REReplace(newResult, "%26", "", "all")# />
<cfset newResult = #REReplace(newResult, "[(][0-9][)]", "", "all")# />
<cfset newResult = #REReplace(newResult, "[(][0-9][0-9][)]", "", "all")# />
<cfset newResult = #REReplace(newResult, "[(][A-Z][A-Z][)]", "", "all")# />
<cfset newResult = #REReplace(newResult, "\^", "", "all")# />
<cfset newResult = #REReplace(newResult, "[ \t][a-z][a-z][ \t]", "@", "all")# />
<cfset newResult = #REReplace(newResult, "[(]", "@(", "all")# />
<cfset newResult = #REReplace(newResult, "[()]", "", "all")# />

<!---loop over the urls and store the teams in a array--->
<cfloop list="#newResult#" index="i" delimiters="#chr(38)#">
    <!--- Sometimes espn has a item with no teams or scores --->
    <!--- If this is the case, a try catch will keep the loop going so it doesn't crash --->
    <cftry>
        <cfset myArray[j] = listgetat("#i#", 2, "=") />
        <cfset j = j + 1 />
    <cfcatch>
        <!--- Don't care what the extra garbage is --->
    </cfcatch>
    </cftry>
</cfloop>

<cfset gamesArray = arrayNew(1) />
<cfset j = 1 />

<cfloop from="3" to="#arraylen(myArray)#" index="m" step="3">
    <cfset gamesArray[j] = #myArray[m]# & "&"  />
    <cfset j =  j + 1 />
</cfloop>

<!---remove garbage character at the end of the array--->
<cfset delete = #arrayDeleteAt(gamesArray, j-1)# />

<!---turn the array back into a list for fun :)--->
<cfset breakUp = #arrayToList(gamesArray,"")# />
<cfset matchArray = arrayNew(1) />
<cfloop list="#breakUp#" index="z" delimiters="#chr(38)#">
    <cftry>
        <cfset matchup = structNew() />
        <cfset matchup.teamOne = trim(listgetat("#z#", 1, "@")) />
         
        <!--- Get the score start and end places --->
        <cfset teamOneScoreStringStart = #findoneof("0123456789",matchup.teamOne)#>
        <cfset teamOneScoreStringEnd = #LEN(matchup.teamOne)#>
        
        <!--- Team 1 Score --->
        <cfif teamOneScoreStringStart EQ 0>
            <cfset matchup.teamOneScore = 0>
        <cfelse>
            <cfset matchup.teamOneScore =#Mid(matchup.teamOne,teamOneScoreStringStart,teamOneScoreStringEnd)#>
        </cfif>
        <!--- Remove Score from teamOne String --->
        <cfif teamOneScoreStringStart NEQ 0>
            <cfset teamOneScoreStringStart = teamOneScoreStringStart - 1>
            <cfset matchup.teamOne = #Mid(matchup.teamOne,1,teamOneScoreStringStart)#>
        </cfif> 
                
        <cfset matchup.teamTwo = trim(listgetat("#z#", 2, "@")) />
        
        <!--- Get the score start and end places --->
        <cfset teamTwoScoreStringStart = #findoneof("0123456789",matchup.teamTwo)#>
        <cfset teamTwoScoreStringEnd = #LEN(matchup.teamTwo)#>
        
        <!--- Team 2 Score --->
        <cfif teamTwoScoreStringStart EQ 0>
            <cfset matchup.teamTwoScore = 0>
        <cfelse>
            <cfset matchup.teamTwoScore =#Mid(matchup.teamTwo,teamTwoScoreStringStart,teamTwoScoreStringEnd)#>
        </cfif>
        <!--- Remove Score from teamTwo String --->
        <cfif teamTwoScoreStringStart NEQ 0>
            <cfset teamTwoScoreStringStart = teamTwoScoreStringStart - 1>
            <cfset matchup.teamTwo = #Mid(matchup.teamTwo,1,teamTwoScoreStringStart)#>
        </cfif> 
     
        <cfset matchup.matchDate = trim(listgetat("#z#", 3, "@")) />
     
    <cfcatch>
        <!--- Don't care, just make sure it doesn't crash --->
    </cfcatch>
    </cftry>
        
    <cfset arrayAppend(matchArray,matchup) />
    
</cfloop>

<!--- LETS SEE OUR LIVE SCORES!!!! --->
<cfif ShowGameDump EQ true>
    <cfdump var="#matchArray#" />
</cfif>    

<!--- Ok lets do something with our live scores, loop through the array and add total score --->
<!--- This will be our new array to hold our information --->
<cfset matchInfoArray = arrayNew(1)>                
<cfloop from="1" to="#arraylen(matchArray)#" index="i">

    <cfset matchupinfo = structNew() />
        
    <!--- Set MATCHDATE --->
    <cfset matchupinfo.matchdate = matchArray[i].matchdate>
    
    <!--- Set TOTAL POINTS --->
    <cfif isnumeric(matchArray[i].teamonescore) AND isnumeric(matchArray[i].teamtwoscore)>
        <cfset matchupinfo.total = (matchArray[i].teamonescore + matchArray[i].teamtwoscore)>
    <cfelse>
        <cfset matchupinfo.total = 0>
    </cfif>
    
    <!--- Set TEAM ONE --->
    <cfset matchupinfo.teamOne = matchArray[i].teamOne />
    
    <!--- Set TEAM ONE SCORE --->
    <cfset matchupinfo.teamOneScore = matchArray[i].teamonescore />
    
    <!--- Set TEAM TWO --->
    <cfset matchupinfo.teamTwo = matchArray[i].teamTwo />
    
    <!--- Set TEAM ONE SCORE --->
    <cfset matchupinfo.teamTwoScore = matchArray[i].teamtwoscore />
    
    <!--- Add game info to the array --->
    <cfset arrayAppend(matchInfoArray,matchupinfo) />
    
</cfloop>

<cfif ShowPassFail EQ true>
    <font size="2" style="color:green;">Live Scoring Template Updated Successfully </font><br /><br /><br /><br />
</cfif>    
<cfset variables.UpdatePassed = true>

<cfcatch type="any">
    <!--- If something went wrong, lets let display an error message and dump the catch --->
    <cfif ShowPassFail EQ true>
        <font size="2" style="color:red;">Live Scoring Template Failed to Update </font><br />
    </cfif>    
    <cfset variables.UpdatePassed = false>
    <cfdump var="#cfcatch#">
</cfcatch>

</cftry>


<!--- Lets make the display a little nicer :) --->
<table width="100%" border="1">

<tr align="center">
    <td>
        Team 1
    </td>
    <td>
        Game Info
    </td>
    <td>
        Team 2
    </td>
</tr>

<cfloop from="1" to="#arraylen(matchInfoArray)#" index="i">

<cfoutput>
     
    <tr>
        <td width="40%" align="left">
            <table border="0" width="100%">
                <tr>    
                    <td align="center" width="60%">
                        <font size="4">
                            #matchInfoArray[i].teamOne# 
                         </font>
                    </td>
                    <td align="right">
                        <font size="6">
                        #matchInfoArray[i].teamOneScore# &nbsp;
                        </font>
                    </td>
                </tr>
            </table>
        </td>
        <td width="20%" align="center">
            Total: #matchInfoArray[i].total#<br />
            #matchInfoArray[i].matchDate#
        </td>
        <td width="40%">
            <table border="0" width="100%">
                <tr>    
                    <td align="left">
                        <font size="6">
                            &nbsp;
                            #matchInfoArray[i].teamTwoScore#
                        </font>
                    </td>
                    <td align="center" width="60%">
                        <font size="4">
                            #matchInfoArray[i].teamTwo#
                        </font>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</cfoutput>
</cfloop>
</table>
php split
2个回答
1
投票

您可以使用 preg_match() 函数

<?php
$string = "Tampa Bay 6 Florida 5 (FINAL - OT)";
$result;

preg_match("/^([^\d]+)(\d+)([^\d]+)(\d+)\s*(\(.*\))$/", $string, $result);

print_r($result);
?>

输出:

Array ( [0] => 坦帕湾 6 佛罗里达 5 (最终 - 加时赛) [1] => 坦帕湾 [2] => 6 [3] => 佛罗里达 [4] => 5 [5] => (最终- 加时))

并且您可以使用预定义的列索引:

$first_team = 1;
$first_team_score = 2;
$result[$first_team];
$result[$first_team_score];

0
投票

在输入字符串中间部分用空格填充的整数上将字符串拆分为正好 5 个部分。

代码:(演示

$string = "Tampa Bay 6 Florida 5 (FINAL - OT)";

var_export(
    preg_split(
        '/ (\d+) /',
        $string,
        5,
        PREG_SPLIT_DELIM_CAPTURE 
    )
);

输出:

array (
  0 => 'Tampa Bay',
  1 => '6',
  2 => 'Florida',
  3 => '5',
  4 => '(FINAL - OT)',
)
© www.soinside.com 2019 - 2024. All rights reserved.