I've following array titled $comments
as follows :
Array
(
[0] => Array
(
[text] => Second Comment Added
)
[1] => Array
(
[text] => This is the long comment added to check thwe size of the comment on the device,if the size is more then add the hyperlink button to go on to the next page
)
[2] => Array
(
[text] => This comment is of two lines need to check more about it
)
[3] => Array
(
[text] => This comment is of two lines need to check more
)
[4] => Array
(
[text] => Uploading Photo for comment <div title="comment_attach_image">
<a title="" title="colorbox" href="https://www.filepicker.io/api/file/CnYTVQdATAOQTkMxpAq4" ><img src="https://www.filepicker.io/api/file/CnYTVQdATAOQTkMxpAq4" height="150px" width="150px" /></a>
<a href="https://www.filepicker.io/api/file/CnYTVQdATAOQTkMxpAq4" class="comment_attach_image_link_dwl">Download</a>
</div>
)
[5] => Array
(
[text] => test
)
[6] => Array
(
[text] => Amit's pic<div class="comment_attach_image">
<a class="group1 cboxElement" href="http://52.1.47.143/file/attachment/2015/03/e55f0f3080eb9828270a7963648a5826.jpeg" ><img src="http://52.1.47.143/file/attachment/2015/03/e55f0f3080eb9828270a7963648a5826.jpeg" height="150px" width="150px" /></a>
<a class="comment_attach_image_link_dwl" href="http://52.1.47.143/feed/download/year_2015/month_03/file_e55f0f3080eb9828270a7963648a5826.jpeg" >Download</a>
</div>
)
[7] => Array
(
[text] => PDF file added<div class="comment_attach_file">
<a class="comment_attach_file_link" href="http://52.1.47.143/feed/download/year_2015/month_03/file_1b87d4420c693f2bbdf738cbf2457d89.pdf" >1b87d4420c693f2bbdf738cbf2457d89.pdf</a>
<a class="comment_attach_file_link_dwl" href="http://52.1.47.143/feed/download/year_2015/month_03/file_1b87d4420c693f2bbdf738cbf2457d89.pdf" >Download</a>
</div>
)
[8] => Array
(
[text] => Just did it...
)
[9] => Array
(
[text] => Profile photo uploaded<div class="comment_attach_image">
<a class="group1 cboxElement" href="http://52.1.47.143/file/attachment/2015/03/a4ea5532b83a56bbbae2fffc80de4fee.png" ><img src="http://52.1.47.143/file/attachment/2015/03/a4ea5532b83a56bbbae2fffc80de4fee.png" height="150px" width="150px" /></a>
<a class="comment_attach_image_link_dwl" href="http://52.1.47.143/feed/download/year_2015/month_03/file_a4ea5532b83a56bbbae2fffc80de4fee.png" >Download</a>
</div>
)
)
I've used XML parsing to parse the HTML data present in above array as follows :
foreach($comments as $key=>$comment) {
$text = strstr($comment['text'], '<div');
if (strlen($text) <= 0) {
$comments[$key]['type_id'] = 'text';
$comments[$key]['url'] = '';
$comments[$key]['text'] = $comment['text'];
} else if($xml = @simplexml_load_string($text)) {
$comments[$key]['type_id'] = substr(strrchr($xml['class'], '_'), 1);
$comments[$key]['url'] = $xml->a['href']->asXML();
$comments[$key]['text'] = strtok($comment['text'], '<');
} else {
continue;
};
}
My code is working only for valid XML data. For invalid XML data it's not working. So, I've decided to use regex instead of simplexml.
So can some one please help me in converting my simplexml code into regex equivalent code?
Thanks in advance.
My final desired array should look as follows :
Array
(
[0] => Array
(
[type] => text
[URL] =>
[text] => Second Comment Added
)
[1] => Array
(
[type] => text
[URL] =>
[text] => This is the long comment added to check thwe size of the comment on the device,if the size is more then add the hyperlink button to go on to the next page
)
[2] => Array
(
[type] => text
[URL] =>
[text] => This comment is of two lines need to check more about it
)
[4] => Array
(
[type] => image
[URL] => https://www.filepicker.io/api/file/CnYTVQdATAOQTkMxpAq4
[text] => Uploading Photo for comment
)
[5] => Array
(
[type] => text
[URL] =>
[text] => test
)
[6] => Array
(
[type] => image
[type] => text
[URL] => http://52.1.47.143/file/attachment/2015/03/e55f0f3080eb9828270a7963648a5826.jpeg
[text] => Amit's pic
)
[7] => Array
(
[type] => file
[URL] => http://52.1.47.143/feed/download/year_2015/month_03/file_1b87d4420c693f2bbdf738cbf2457d89.pdf
[text] =>
)
[8] => Array
(
[type] => text
[URL] =>
[text] => Just did it...
)
[9] => Array
(
[type] => image
[URL] => http://52.1.47.143/file/attachment/2015/03/a4ea5532b83a56bbbae2fffc80de4fee.png
[text] => PDF file added
)
)
If you execute my code you could not pass few of the elements from above array since it contains invalid xml. But I want to parse them too. That's my real issue.