Catching text within a PHP ul list

1

Hello, I'm creating a project that I need to get some texts from a list on another site, I'll show you how I need it;

I'm using file_get_contents ;

$url = "www.site.com";
$html = file_get_contents($url);
$getTextoList = "";
preg_match_all($getTextoList, $html, $GetTexto);
$GetTexto = str_replace(" ", ", ", $$GetTexto[1][0]);

The site has a list like this:

<div class="listdoTexto">
<ul>                                                                    
<li class="texto"><a href="textolink">texto</a></li>
<li class="texto"><a href="textolink">texto</a></li>
<li class="texto"><a href="textolink">texto</a></li>
<li class="texto"><a href="textolink">texto</a></li>
</ul>
</div>

I wanted to get only the "text" that is between <a></a> of this list.

    
asked by anonymous 30.03.2018 / 18:21

1 answer

0

You can use regex in preg_match_all :

|<div class="listdoTexto">[\w\W]*<\/ul>|

Where [\w\W]* will get any character between <div class="listdoTexto"> and <\/ul> :

\w -> qualquer caractere alfanumérico e underscore "_")
\W -> qualquer caractere NÃO alfanumérico e underscore "_")
*  -> uma ou quantas ocorrências houverem entre o último </ul> até <div class="listdoTexto">

This will return me an Array at index [0] , where it contains the result I want. In this case I convert Array to string with implode :

$GetTexto = implode(",", $GetTexto[0]);

However we may have a problem if there is more list in HTML. Example:

         |→ <div class="listdoTexto">
         |    <ul>
Só quero |       <li class="texto"><a href="textolink">texto1</a></li>
pegar    |       <li class="texto"><a href="textolink">texto2</a></li>
esta     |       <li class="texto"><a href="textolink">texto3</a></li>
parte... |       <li class="texto"><a href="textolink">texto4</a></li>
         |→    </ul>
            </div>

           <div class="listdoTexto2">
              <ul>                
..mas o          <li class="texto"><a href="textolink">texto5</a></li>
regex irá          <li class="texto"><a href="textolink">texto6</a></li>
até aqui →    </ul>
           </div>

That is, the result of $GetTexto after implode would be this:

<div class="listdoTexto">
   <ul>
      <li class="texto"><a href="textolink">texto1</a></li>
      <li class="texto"><a href="textolink">texto2</a></li>
      <li class="texto"><a href="textolink">texto3</a></li>
      <li class="texto"><a href="textolink">texto4</a></li>
  </ul>
</div>

<div class="listdoTexto2">
   <ul>
      <li class="texto"><a href="textolink">texto5</a></li>
      <li class="texto"><a href="textolink">texto6</a></li>
  </ul>

As I want to get only the first </ul> , I can use substr with strpos :

$GetTexto = substr($GetTexto, 0, strpos($GetTexto, "</ul>"));

The result now is this:

<div class="listdoTexto">
   <ul>
      <li class="texto"><a href="textolink">texto1</a></li>
      <li class="texto"><a href="textolink">texto2</a></li>
      <li class="texto"><a href="textolink">texto3</a></li>
      <li class="texto"><a href="textolink">texto4</a></li>
  </ul>

As I only want the text, I use strip_tags to delete the tags:

$GetTexto = strip_tags($GetTexto);

Will only return text, but with line breaks and possible spaces before, after or between texts:

texto1
texto2
texto3
texto4

I can use preg_replace with trim to replace line breaks and unwanted spaces with ,, , which will then be used later in a replace :

$GetTexto = preg_replace("/\s{2,}|\n/", ",,", trim($GetTexto));

Now we have:

texto1,,texto2,,texto3,,texto4

Now to separate the texts with a comma and space, you can use str_replace replacing ,, :

$GetTexto = str_replace(",,", ", ", $GetTexto);

Final result:

texto1, texto2, texto3, texto4
  

Despite arriving at the final result, I do not know if this would be the best   approach. There may be a method using Document Object   Model more efficient, but I hope it helps.

Code:

$url = "http://www.site.com";
$html = file_get_contents($url);

$getTextoList = '|<div class="listdoTexto">[\w\W]*<\/ul>|';
preg_match_all($getTextoList, $html, $GetTexto);

$GetTexto = implode(",", $GetTexto[0]);
$GetTexto = substr($GetTexto, 0, strpos($GetTexto, "</ul>")); // 
$GetTexto = strip_tags($GetTexto);
$GetTexto = preg_replace("/\s{2,}|\n/", ",,", trim($GetTexto));
$GetTexto = str_replace(",,", ", ", $GetTexto);

Try Ideone

    
31.03.2018 / 00:20