我有一系列标题为的评论$comments
。实际的数组可能很大,可能包含数百个这样的元素。为了您的理解,我仅向其中添加了10个元素,如下所示:
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>
)
)
现在,我要实现的是根据[text]
上述数组的键中包含的值来标识注释的类型。
让我在示例的帮助下向您解释。让我们考虑一下以下元素$comments[9][text]
它包含以下值(即HTML代码):
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>
我要从此HTML数据中提取的第一件事是字符串中的结尾字符串<div class="comment_attach_image">
。
换句话说,我想从字符串中提取单词图像<div class="comment_attach_image">
。这将表明此注释的类型为image。
然后我只想要图像的URL,即http://52.1.47.143/file/attachment/2015/03/a4ea5532b83a56bbbae2fffc80de4fee.png
(的src属性值<img>
)。
现在,这两个值都应插入具有键类型和URL的新数组中,如下所示:
Array
(
[9] => Array
(
[type] => image
[URL] => http://52.1.47.143/file/attachment/2015/03/a4ea5532b83a56bbbae2fffc80de4fee.png
[text] =>
)
)
另一个$comments[7][text]
不是image类型而是file类型的数组元素也会发生相同的情况,如下所示:
它包含以下值(即HTML代码):
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>
我要从此HTML数据中提取的第一件事是字符串中的结尾字符串<div class="comment_attach_file">
。
换句话说,我想从字符串中提取单词文件<div class="comment_attach_file">
。这将表明此注释的类型为file。
然后,我只想要文件的URL,即http://52.1.47.143/feed/download/year_2015/month_03/file_1b87d4420c693f2bbdf738cbf2457d89.pdf
(的hre属性值<a>
)。
现在,这两个值都应插入具有键类型和URL的新数组中,如下所示:
Array
(
[9] => Array
(
[type] => file
[URL] => http://52.1.47.143/feed/download/year_2015/month_03/file_1b87d4420c693f2bbdf738cbf2457d89.pdf
[text] =>
)
)
对于不包含任何HTML的其他元素,即包含简单文本的内容,则不会发生任何事情。对于他们,新数组中的条目应如下所示:
让我们考虑一下元素 $comments[0][text]
它包含以下值:
Second Comment Added
因此,新数组中的条目应如下所示:
Array
(
[0] => Array
(
[type] => text
[URL] =>
[text] => Second Comment Added
)
)
这样,包含注释类型及其内容的新数组应该以一种有效的方式生成。
有人可以在这方面帮助我吗?
如果您对我所面临的问题有任何疑问,请告诉我。
任何帮助将不胜感激。
提前致谢。
等待您的宝贵答复。
最终所需的数组应如下所示:
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
)
)
$comments = array(
array(
'text' => 'Second Comment Added'
),
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'
),
array(
'text' => 'This comment is of two lines need to check more about it'
),
array(
'text' => 'This comment is of two lines need to check more '
),
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>'
),
array(
'text' => 'test'
),
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>'
),
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>'
),
array(
'text' => 'Just did it...'
),
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>'
)
);
$newArray = array();
foreach($comments as $comment) {
$text = strstr($comment['text'], '<div');
if (strlen($text) <= 0) {
$newArray[] = array(
'type' => 'text',
'url' => '',
'text' => $comment['text']
);
} else if($xml = @simplexml_load_string($text)) {
$newArray[] = array(
'type' => substr(strrchr($xml['class'], '_'), 1),
'url' => $xml->a['href']->asXML(),
'text' => strtok($comment['text'], '<')
);
} else {
continue;
};
}
var_dump($newArray);
输出:
array (size=9)
0 =>
array (size=3)
'type' => string 'text' (length=4)
'url' => string '' (length=0)
'text' => string 'Second Comment Added' (length=20)
1 =>
array (size=3)
'type' => string 'text' (length=4)
'url' => string '' (length=0)
'text' => string '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' (length=154)
2 =>
array (size=3)
'type' => string 'text' (length=4)
'url' => string '' (length=0)
'text' => string 'This comment is of two lines need to check more about it' (length=56)
3 =>
array (size=3)
'type' => string 'text' (length=4)
'url' => string '' (length=0)
'text' => string 'This comment is of two lines need to check more ' (length=48)
4 =>
array (size=3)
'type' => string 'text' (length=4)
'url' => string '' (length=0)
'text' => string 'test' (length=4)
5 =>
array (size=3)
'type' => string 'image' (length=5)
'url' => string ' href="http://52.1.47.143/file/attachment/2015/03/e55f0f3080eb9828270a7963648a5826.jpeg"' (length=88)
'text' => string '' (length=0)
6 =>
array (size=3)
'type' => string 'file' (length=4)
'url' => string ' href="http://52.1.47.143/feed/download/year_2015/month_03/file_1b87d4420c693f2bbdf738cbf2457d89.pdf"' (length=101)
'text' => string '' (length=0)
7 =>
array (size=3)
'type' => string 'text' (length=4)
'url' => string '' (length=0)
'text' => string 'Just did it...' (length=14)
8 =>
array (size=3)
'type' => string 'image' (length=5)
'url' => string ' href="http://52.1.47.143/file/attachment/2015/03/a4ea5532b83a56bbbae2fffc80de4fee.png"' (length=87)
'text' => string '' (length=0)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句