Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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 - Fatal编程技术网

使用PHP获取html标记

使用PHP获取html标记,php,html,Php,Html,我有: 接下来,我想在变量$body中将属性url更改为“/test aaa.php-123”、“/test bbb.php-123”等。因此,我希望收到: $body = ' <div id="one"> <div id="two"> <div class="sub"> <span class="text"><a class="here" href="/aaa.php">ttt<

我有:

接下来,我想在变量$body中将属性url更改为“/test aaa.php-123”、“/test bbb.php-123”等。因此,我希望收到:

$body = '

<div id="one">
    <div id="two">
        <div class="sub">
            <span class="text"><a class="here" href="/aaa.php">ttt</a></span>
        </div>
        <span class="f">aa</span>
        <div class="sub2">
            <a class="here" href="/bbb.php">ttt</a>
            <div>
                <a class="here" href="/ttt.php">ttt</a>
            </div>
            <a class="here" href="/ddd.php">ttt</a>
        </div>
        <div class="sub">
            <a class="here" href="/zzz.php">ttt</a>
        </div>
    </div>
</div>

';
Array
(
    [0] => /aaa.php
    [1] => /bbb.php
    [2] => /ttt.php
    [3] => /ddd.php
    [4] => /zzz.php
)
$body=
aa
';

我可以用javascript编写,但我必须使用PHP。有可能吗?

有一个易于使用的工具,它对这些东西非常有用:

代码应该是这样的:

$body = '

<div id="one">
    <div id="two">
        <div class="sub">
            <span class="text"><a class="here" href="/test-aaa.php-123">ttt</a></span>
        </div>
        <span class="f">aa</span>
        <div class="sub2">
            <a class="here" href="/test-bbb.php-123">ttt</a>
            <div>
                <a class="here" href="/test-ttt.php-123">ttt</a>
            </div>
            <a class="here" href="/test-ddd.php-123">ttt</a>
        </div>
        <div class="sub">
            <a class="here" href="/test-zzz.php-123">ttt</a>
        </div>
    </div>
</div>

';
替换它 HTML文件

foreach($matches[1] as $link)
{
   echo $link;
}

为什么要将HTML保存到php变量?
$new_body = preg_replace('/<a [^>]*href="(.+)"/', '$1-123', $body);

echo $new_body;
preg_match_all('/<a [^>]*href="(.+)"/', $body, $matches);
var_dump($matches);

array (size=2)
  0 => 
    array (size=5)
      0 => string '<a class="here" href="/aaa.php"' (length=31)
      1 => string '<a class="here" href="/bbb.php"' (length=31)
      2 => string '<a class="here" href="/ttt.php"' (length=31)
      3 => string '<a class="here" href="/ddd.php"' (length=31)
      4 => string '<a class="here" href="/zzz.php"' (length=31)
  1 => 
    array (size=5)
      0 => string '/aaa.php' (length=8)
      1 => string '/bbb.php' (length=8)
      2 => string '/ttt.php' (length=8)
      3 => string '/ddd.php' (length=8)
      4 => string '/zzz.php' (length=8)
foreach($matches[1] as $link)
{
   echo $link;
}
<html>
<head>
<title>Example site</title>
</head>
<body>
<div id="one">
    <div id="two">
        <div class="sub">
            <span class="text"><a class="here" href="/test-aaa.php-123">ttt</a></span>
        </div>
        <span class="f">aa</span>
        <div class="sub2">
            <a class="here" href="/test-bbb.php-123">ttt</a>
            <div>
                <a class="here" href="/test-ttt.php-123">ttt</a>
            </div>
            <a class="here" href="/test-ddd.php-123">ttt</a>
        </div>
        <div class="sub">
            <a class="here" href="/test-zzz.php-123">ttt</a>
        </div>
    </div>
</div>
</body>
</html>
<?php
include('simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('HTML FILE.html');

// Get all links
foreach($html->find('a') as $element){
       $links[] = $element->href;
}
print_r($links);
?>
Array
(
    [0] => /test-aaa.php-123
    [1] => /test-bbb.php-123
    [2] => /test-ttt.php-123
    [3] => /test-ddd.php-123
    [4] => /test-zzz.php-123
)