Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中检查前一个字符串是否存在于另一个字符串中,并从HTML数据中获取文件名?_Php_Html_Arrays_String_Associative Array - Fatal编程技术网

如何在PHP中检查前一个字符串是否存在于另一个字符串中,并从HTML数据中获取文件名?

如何在PHP中检查前一个字符串是否存在于另一个字符串中,并从HTML数据中获取文件名?,php,html,arrays,string,associative-array,Php,Html,Arrays,String,Associative Array,我有一个名为$comments的数组关联数组,如下所示: Array ( [0] => Array ( [text] => Uploading Photo for comment <div class="comment_attach_image"> <a title="colorbox" href="https://www.filepicker.io/api/file/CnY

我有一个名为
$comments
的数组关联数组,如下所示:

    Array
    (
    [0] => Array
            (
                [text] => Uploading Photo  for comment <div class="comment_attach_image">

    <a 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>                
            )
    [1] => Array
            (
                [text] => <div class="comment_attach_file">

    <a class="comment_attach_file_link" href="https://www.filepicker.io/api/file/pxRBwNBcSueP0hf1meOI" >pxRBwNBcSueP0hf1meOI</a>

    <a class="comment_attach_file_link_dwl"  href="https://www.filepicker.io/api/file/pxRBwNBcSueP0hf1meOI" >Download</a>
    </div>
            )
     [2] => Array
            (  
                [text] => This comment is of two lines need to check more about it                
            )
    )
注意:请看两个元素的HTML中的细微差别。请考虑这些差异,以便完成我提到的两个操作。
谢谢

正如我在评论中所说,使用HTML解析器时,此任务(HTML解析)更易于管理。不要为此使用字符串函数。这就是它们的设计目的

在本例中,使用了
DOMDocument
类:

$comments = array(
    array('text' => 'Uploading Photo  for comment <div class="comment_attach_image">

        <a 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' => '<div class="comment_attach_file">

        <a class="comment_attach_file_link" href="https://www.filepicker.io/api/file/pxRBwNBcSueP0hf1meOI" >pxRBwNBcSueP0hf1meOI</a>

        <a class="comment_attach_file_link_dwl"  href="https://www.filepicker.io/api/file/pxRBwNBcSueP0hf1meOI" >Download</a>
        </div>'
    ),
);

$data = array();

foreach($comments as $c) {
    $temp = array();
    $dom = new DOMDocument;
    @$dom->loadHTML(trim($c['text']));
    $first = $dom->getElementsByTagName('body')->item(0)->firstChild;


    $file = $first->parentNode->getElementsByTagName('a')->item(0);
    $url = $file->getAttribute('href');


    if($first->tagName != 'div') {
        // not div
        $not_div = $first->parentNode->getElementsByTagName('div')->item(0);
        $type = explode('_', $not_div->getAttribute('class'));
        $type = end($type);
        $temp['type_id'] = $type;
        $temp['url'] = $url;
        $temp['file_name'] = basename($url);
        $temp['text'] = $c['text'];

    } else {
        // div
        $type = explode('_', $first->getAttribute('class'));
        $type = end($type);
        $temp['type_id'] = $type;
        $temp['url'] = $url;
        $temp['file_name'] = basename($url);
        $temp['text'] = '';
    }

    $data[] = $temp;
}

echo '<pre>', print_r($data, 1), '</pre>';
$comments=array(
数组('text'=>'上传照片以供评论
'
),
数组('text'=>'
'
),
);
$data=array();
foreach(注释为$c){
$temp=array();
$dom=新的DOMDocument;
@$dom->loadHTML(trim($c['text']);
$first=$dom->getElementsByTagName('body')->item(0)->firstChild;
$file=$first->parentNode->getElementsByTagName('a')->项(0);
$url=$file->getAttribute('href');
如果($first->tagName!=“div”){
//非div
$not_div=$first->parentNode->getElementsByTagName('div')->项(0);
$type=explode(“”,$not_div->getAttribute('class');
$type=结束($type);
$temp['type_id']=$type;
$temp['url']=$url;
$temp['file_name']=basename($url);
$temp['text']=$c['text'];
}否则{
//div
$type=explode(“”,$first->getAttribute('class');
$type=结束($type);
$temp['type_id']=$type;
$temp['url']=$url;
$temp['file_name']=basename($url);
$temp['text']='';
}
$data[]=$temp;
}
回显“”,打印($data,1),“”;

在解析HTML字符串时,应经常考虑使用HTML解析器、 DOMDOCT/<代码>,特别感谢您的帮助,但我没有获得第二个数组元素的文件名。代码中还缺少前面的文本检查和新数组键创建。如果你再读一遍我的问题,你就会意识到答案中遗漏了什么,称之为完整答案。如果你能给我一个完整的答案,那将对我很有帮助。使用最后一个数组。@PHPNut哦,好的,应该不会有太大的变化,我会在一点时间内修改它:谢谢。我的最终输出数组应该包含这些文本和文件名键,这就是我的最终目标。因此,您需要相应地进行更改。再次感谢。@PHPNut我已经做了必要的更改(如果我理解正确的话),无论如何,我无法设置演示代码板。viper-7.com现在似乎已关闭。非常感谢您的大力帮助。只是小小的修正。在[type_id]键中,对于第一个元素,它应该是图像(即字符串注释_attach_文件中的最后一个单词),对于第二个元素,它应该是文件(即字符串注释_attach_文件中的最后一个单词)。对于第一个数组元素,[url]和[file_name]键也是空的。事实上,它们应该包含相应的文件名和URL。你能对你的代码做些小的修改吗?再次非常感谢。
$comments = array(
    array('text' => 'Uploading Photo  for comment <div class="comment_attach_image">

        <a 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' => '<div class="comment_attach_file">

        <a class="comment_attach_file_link" href="https://www.filepicker.io/api/file/pxRBwNBcSueP0hf1meOI" >pxRBwNBcSueP0hf1meOI</a>

        <a class="comment_attach_file_link_dwl"  href="https://www.filepicker.io/api/file/pxRBwNBcSueP0hf1meOI" >Download</a>
        </div>'
    ),
);

$data = array();

foreach($comments as $c) {
    $temp = array();
    $dom = new DOMDocument;
    @$dom->loadHTML(trim($c['text']));
    $first = $dom->getElementsByTagName('body')->item(0)->firstChild;


    $file = $first->parentNode->getElementsByTagName('a')->item(0);
    $url = $file->getAttribute('href');


    if($first->tagName != 'div') {
        // not div
        $not_div = $first->parentNode->getElementsByTagName('div')->item(0);
        $type = explode('_', $not_div->getAttribute('class'));
        $type = end($type);
        $temp['type_id'] = $type;
        $temp['url'] = $url;
        $temp['file_name'] = basename($url);
        $temp['text'] = $c['text'];

    } else {
        // div
        $type = explode('_', $first->getAttribute('class'));
        $type = end($type);
        $temp['type_id'] = $type;
        $temp['url'] = $url;
        $temp['file_name'] = basename($url);
        $temp['text'] = '';
    }

    $data[] = $temp;
}

echo '<pre>', print_r($data, 1), '</pre>';