Php explode using 1 fixed character and 2 random numbers

ぐ巨炮叔叔 提交于 2019-12-23 05:40:06

问题


I have created a task that will read from an xml file, and then store information to a database. During the process it strips out unnecessary information, and changing other parts.

When reading the XML file, I get information as follows:

<time>2/1/2016 16:49:15</time>
<type>GOALS</type>
<event>FC HALIFAX TOWN 2-2 Lincoln City</event>

I am getting stuck when working with the 'event'.

I want to explode the event, so that I am left with:

$team[0] = "FC HALIFAX TOWN"
$team[1] = "Lincoln City"

I can't use '-' and then remove the last character from $team[0] and the first character from $team[1] as there are some teams that use '-' in their name, such as 'Maccabi Tel-Aviv'.

So I am trying to find a way to explode and it being able to find the numbers, rather than letters.

Anyone able to help?


回答1:


As match results are different, I suggest using regexps and preg_split:

$r = preg_split("/ (\d+)\-(\d)+ /", "FC HALIFAX TOWN 2-2 Lincoln City");
print_r($r); // outputs:  Array ( [0] => FC HALIFAX TOWN [1] => Lincoln City ) 

I specially added spaces around match results so as to make regexp more precise.




回答2:


See this code.

$team = preg_split("/[\d]\-[\d]+/", "FC HALIFAX TOWN 2-8 Lincoln City");
print_r($team); 

// output: Array ( [0] => FC HALIFAX TOWN [1] => Lincoln City )



回答3:


You can still explode "-" just add up the last string until the result array is at end.



来源:https://stackoverflow.com/questions/34676490/php-explode-using-1-fixed-character-and-2-random-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!