-1

I have:

$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>

';

How can i get from this all href from tag "a" to the array? I would like receive:

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

AND NEXT i would like change attribute url to "/test-aaa.php-123", "/test-bbb.php-123" etc in variable $body. So i would like receive:

$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>

';

I can make this in javascript, but i must use for this PHP. Is possible?

arclota
  • 21

3 Answers3

2

Replace it

$new_body = preg_replace('/<a [^>]*href="(.+)"/', '$1-123', $body);

echo $new_body;

Or match href links

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

Output

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)

Access index

foreach($matches[1] as $link)
{
   echo $link;
}
Bora
  • 10,529
  • 5
  • 43
  • 73
1

There is a tool that is easy to use and it works really great for this stuff: http://simplehtmldom.sourceforge.net/

the code should be something like this:

// Include the library
include('simple_html_dom.php');
foreach($body->find('a') as $a){
   $links[] = $a->href;
}
Andy
  • 2,892
  • 2
  • 26
  • 33
1

the HTML file

<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>

You have to download and include html dom parser to get html tags. Download it from this web address. http://simplehtmldom.sourceforge.net/

This is the PHP script to get links on your document

<?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);
?>

Output:

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
)
Suresh Peiris
  • 396
  • 2
  • 14