1

我有一个$str这样定义的字符串:

$str = "Horosho byt' tihoney 23 Sen 2012 14:00 | 20:20 | 00:30 24 Sen 2012 09:30 | 14:00 Obitel' zla: Vozmezdie 3D 23 Sen 2012 16:10 | 20:25 | 22:25 Osobo opasny 23 Sen 2012 18:00 24 Sen 2012 11:30 | 18:00 25 Sen 2012 11:30 | 18:00 26 Sen 2012 11:30 | 18:00 Patrul' 23 Sen 2012 20:10 | 22:20 24 Sen 2012 11:50 | 20:10"

我想用<br>标签包围单词;例如,Horosho byt' tihoney会变成<br>Horosho byt' tihoney<br>,并且Obitel' zla: Vozmezdie 3D会变成<br>Obitel' zla: Vozmezdie 3D<br>

到目前为止我所拥有的:

preg_replace("/(?<=\s)([\p{L}*?]{3,})(?=\s[0-9]{2}\s)/u", "<br>$1<br>", $str);
4

1 回答 1

0

Alright, so here is my solution. It's not exactly what you specified, but it solves your problem. The dates and times seem much more regular than the names (because the latter might contain digits, apostrophes and colons... and who know what more). So I figured, enclosing the dates and times in <br/> would be just as good (of course you have to add another <br/> right at the start and remove one from the end, but that's less of a problem, I guess. So here is the regex that finds strings of dates and times and encloses them (and their surrounding spaces) in <br/>:

$newStr = preg_replace("/(\s\d{1,2}\s[A-Z][a-z][a-z]\s\d{4}|\s\d{2}:\d{2}|\s\|)+\s?/", "<br/>$0<br/>", $str);

I assumed that the day of your dates might have 1 or 2 digits (couldn't tell from the example string) and that all your months are 1 upper-case and 2 lower-case characters.

EDIT: I added a ? at the end to make it work with the last time in the string.

于 2012-09-23T17:55:27.963 回答