0

I have the following PHP string

$string = "Hello World<br>- 8/7/2013<br>Hello World";

So basically, I need to get the information between a dash and space (-) and the closest line break tag. Your help is very much appreciated! I looked for hours but haven't been successful. I don't think that preg_replace would help either.

4

3 回答 3

3

strpos查找子字符串的索引(可选地在指定索引之后),因此:

$start = strpos($string, '- ') + 2;
$end   = strpos($string, '<br>', $start);
$part  = substr($string, $start, $end - $start);

达达!

于 2013-08-07T17:05:17.697 回答
1

您可以使用preg_match

if(preg_match("~-\s(.*?)<br>~", $string, $matches)) {
    $data = $matches[1];
}
于 2013-08-07T17:06:30.557 回答
0

尝试这个

function getData($haystack) {
    $dash = strpos($haystack, "- ") + 2;
    $br = strpos($haystack, "<br>", $dash);
    return substr($haystack, $dash, $br - $dash);
}
于 2013-08-07T17:06:12.633 回答