Question

I have the following string

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

I want to extract all the information between the dash + space ("- ") and the next line break tag. Is that possible? I've been researching Google for hours but no luck. I'm thinking that I need an array of strpos of the "- " and the following line break tag and then batch substr them. But of course if you can do this any other way, that would be so much appreciated!

Was it helpful?

Solution

I've updated my answer to handle multiple occurances.

You can do this with a simple regular expression:

preg_match_all("@- (.*)<br />@U", $string, $matches);
print_r($matches[1]);

The above will print

Array
(
    [0] => 8/7/2013
    [1] => 8/6/2013
)

This matches the pattern - (.*)<br /> with (.*) meaning anything. The @s here work as delimiters to separate the actual pattern with the modifiers (in this case U meaning an ungreedy match).

OTHER TIPS

regular expressions will give you the most control, but here is a quick demo using strpos and substr to get what you asked for:

$strStart = strpos($string, '- ');
$strLength = strpos($string, '<br') - $strStart;
substr( $string, $strStart, $strLength );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top