Possible Duplicate:
Robust, Mature HTML Parser for PHP
I’m trying to grab the first sentence of a string and the first image html instance.
$description = preg_split('/<img/', $item->description,null,PREG_SPLIT_DELIM_CAPTURE);
I’m able to returns an array but it’s removing the <img from it’s values which is needed. I’ve tried using flags but can’t get the return I’m looking for which need to include the delimiter itself. I know to grab the first sentence I should be able to split by period or
String:
<p>First sentence here comes. Second sentence here it is. One more sentence. </p> <img alt="amj" src="https://domain.com/images7.jpg" /> <img alt="Ea" src="http://domain.com/images3.jpg" /> <img alt="amj" src="https://domain.com/images7.jpg" /> <img alt="amj" src="https://domain.com/images7.jpg" />
If you make use of
PREG_SPLIT_DELIM_CAPTUREyou need to provide a capture within the regular expression pattern used withpreg_split.In your current pattern:
There is mothing to capture, that is why you see it removed (Demo):
However, if you create a capture out of it, it will be captured:
Result (Demo):
As you can see,
preg_splitdoes it’s documented job and will add another split per each capture of the first capturing supgroup (it will only take the first). You then might need to extend it across the full tag, which has been outline in different other html-like-string-regex questions, for example (limited as usual with regular expressions, so blame that you use preg_* functions instead of a HTML parser if you run into issues, not the pattern itself:Result (Demo):
You would make your code more stable by using a standard HTML parser.