-2

I'm coding little script and I've faced this problem

now I have this HTML code

<div class="domains">
       <ul>
  <li class="noMessages">
<a href="select-admin-domain.do?domain=ex1.com">ex1.com</a>
          </li>
<li class="noMessages">
<a href="select-admin-domain.do?domain=ex2.com">ex2.com</a>
      </li>
<li class="cpCurrentDomain noMessages">
<a href="select-admin-domain.do?domain=ex3.com">ex3.com</a>
      </li>
<li class="noMessages">
<a href="select-admin-domain.do?domain=ex4.com">ex4.com</a>
          </li>
        
       </ul>
      </div>
now i want to extract the text from all this html tag using PHP
<a href="select-admin-domain.do?domain=ex1.com">ex1.com</a>
<a href="select-admin-domain.do?domain=ex2.com">ex2.com</a>
<a href="select-admin-domain.do?domain=ex3.com">ex3.com</a>
<a href="select-admin-domain.do?domain=ex4.com">ex4.com</a>

so the output become ex1.com ex2.com etc..

i've make this code

<?php
function GetStr($string,$start,$end){
    
        
    
    $str = explode($start, $string);
    $str = explode($end, $str[1]);
    echo $str[0];
    
    
}
$ss= getStr($htmlcode,'<a href="select-admin-domain.do?domain=','">');

echo $ss;

it works good but it only gives me the first output ex1.com and I want to echo all of them not just 1

ORVX
  • 35
  • 4

2 Answers2

1

You can write a simple regex to match <a> tags containing a link to select-admin-domain.do

For example:

$re = '/<a href="select-admin-domain.do.*?">(.*?)<\/a>/';
if (preg_match_all($re, $html, $matches, PREG_SET_ORDER, 0)) {
    var_dump(array_column($matches, 1));
}

// Outputs
//    array(4) {
//        [0] =>
//      string(7) "ex1.com"
//        [1] =>
//      string(7) "ex2.com"
//        [2] =>
//      string(7) "ex3.com"
//        [3] =>
//      string(7) "ex4.com"
//    }
atymic
  • 3,093
  • 1
  • 13
  • 26
0

If you have in your $string var a (string) containig a html code and you want to get a each link href, or text you can use this code too:

//$string var containt html
echo strip_tags($string);

//output
ex1.com ex2.com ex3.com ex4.com
Serghei Leonenco
  • 3,478
  • 2
  • 8
  • 16