如何从包含HTML代码的数组元素中提取特定部分,并使用提取的内容创建一个新数组?

风扇

我有一系列标题为的评论$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&#039;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&#039;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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Excel:从二维数组中提取所有部分匹配项,并创建一个包含结果的列表

在Spark中,如何从结构中提取数组并使用它创建一个新字段

如何从Perl中仅包含一个元素出现且仅包含一个对应元素出现的数组中提取数据

如何使用nodejs在mongoose中的另一个数组中提取或删除数组的元素?

如何使用 AppleScript 创建一个数组数组并提取一个项目?

如何使用另一个数组按键/值从数组中提取对象

如何从csv文件中提取一个numpy数组?

Jmeter:如何从json数组中提取第一个元素

使用Jolt从第一个数组元素中提取值

如何从对象中提取pin属性并在JavaScript中创建一个pin数组?

如何从数组中获取特定元素并基于数组键值的比较创建一个新数组?

如何使用字典数组上的条件从同一字典中的另一个数组中提取元素?

如何提取数组的第一个找到的元素?

使用一个数组的索引从列表中提取要附加到新数组中的值(Python)

C#-如何从数组列表中提取一个特定数组成员的列表

从嵌套的数组中提取一个数组

从数组中提取特定元素以创建范围

从 numpy 数组中提取数据以创建另一个数组

从数组中提取对象以创建另一个数组

如何从一个数组中提取数组并将它们转换为对象并将它们放入一个新数组中?

如何从一个数组中提取零并将非零部分保存到另一个数组?

如何从一个新的数组中提取所有的零和非零值?

如何基于另一个数组中的条件从关联数组中提取?

如何从一个对象数组中提取所有可能匹配的对象数组?

使用打字稿从数组中提取除第一个之外的所有元素类型

从多维数组中提取特定元素

使用es6数组方法从数组中提取第一个对象

如何从数据框中的列中提取字符串模式,并创建一个包含提取的字符串的新数据框列?

创建一个能够从目录名称中提取特定元素的函数?