简体   繁体   English

从preg_match_all中的html代码获取元素的内容

[英]Get content of elements from the html code in preg_match_all

I have an old database where the articles are located. 我有文章所在的旧数据库。 In each article description in the "text" field of MySQL there are links in which the parameters are included. 在MySQL的“文本”字段中的每篇文章描述中,都有包含参数的链接。 I want to get them out using the preg_match_all function 我想使用preg_match_all函数将它们删除

Example element in description: 说明中的示例元素:

<a href="[xxxx][1] " cat_id="[xxxx][2]" tab-id="[xxxx]3">[xxxx][4]</a>

I wrote a code that does not work, how can I pull out only these elements, omitting the rest of the content? 我写的代码行不通,如何才能仅提取这些元素,而忽略其余内容?

my code: 我的代码:

<?php
$desc = '<a href="http://mywebsite.com" cat_id="156" tab-id="3">My name</a>';
preg_match_all('/<a href="(?P<href>.*)" cat_id="(?P<cat_id>.*)" tab-id="(?P<tab_id>.*)">(?P<name>.*)</a>/', $desc, $return); 
print_r($return);
?>
<?php

$data='<a href="http://mywebsite.com" cat_id="156" tab-id="3">My name</a>';


preg_match_all("/(?:(?:\"(?:\\\\\"|[^\"])+\")|(?:\'(?:\\\\' | [^\'])+\'))/is", $data, $match);

echo '<pre>';

print_r($match);
echo(strip_tags($data));

The preg_match_all will look for every value that is enclosed in double quotes and create an array with them. preg_match_all将查找用双引号引起来的每个值,并使用它们创建一个数组。 The output is like this : 输出是这样的:

Array
(
    [0] => Array
        (
            [0] => "http://mywebsite.com"
            [1] => "156"
            [2] => "3"
        )

)

For getting the name inside the html tags you can use the strip_tags function to remove every HTML code and get only the text of it. 为了在html标记中获取名称,您可以使用strip_tags函数删除每个HTML代码并仅获取其文本。 The output will be : 输出将是:

My name

You might also use DOMDocument 您可能还会使用DOMDocument

$desc = '<a href="http://mywebsite.com" cat_id="156" tab-id="3">My name</a>';
$dom = new DOMDocument();
$dom->loadHTML($desc);
$elm = $dom->getElementsByTagName("a");
echo $elm->item(0)->getAttribute("cat_id");
echo "<br>";
echo $elm->item(0)->getAttribute("tab-id");
echo "<br>";
echo $elm->item(0)->nodeValue;

That would give you: 那会给你:

156
3
My name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM