Search code examples
phptagsfindsimple-html-domcapture

PHP Simple HTML DOM - Get text inside rare tag


my problem is that I can not capture the text within this tag:

<p class="name">
    "                        Eau de Toillete for Men, Spray 110ml     "       </p>

As you can see, the text is in quotes

" Eua of toilette for Men, 110ml Spray "

This is my code:

$pos1 = ".h2marca";
$pos2 = "[id=landing-submarca-perfume] h1";
$pos3 = "[class=name]";
$pos4 =".price";
$contador = 0


while (!empty($titulo3 = trim($html2->find($pos3,$contador)->plaintext)))
    {
        $titulo1 = trim($html2->find($pos1,0)->plaintext);

        $titulo2 = trim($html2->find($pos2,0)->plaintext);

        $titulo3 = trim($html2->find($pos3,$contador)->plaintext);
        $titulo3 = str_replace("for Women, ","",$titulo3);
        $titulo3 = str_replace("for Men, ","",$titulo3);

        $titulo= $titulo1 . " " . $titulo2 . " " . str_replace("."," ",$titulo3);   
        $precio = trim($html2->find($pos4,$contador)->innertext);

    $contador++;
    }

I need use "$contador" because there are others adds in this webs and need capture all.

$título3 capture an empty space.

I need to capture text without removing the $contador variable

Can you help me? this is the example web http://www.fundgrube.es/es/perfumes/aramis/aramis.html

Thanks!.


Solution

  • A bit round the houses but this might work:

      $split_this = '<p class="name">
          "                        Eau de Toillete for Men, Spray 110ml     "       </p>';
    
      $split_this = strip_tags($split_this, '');
      $split_this = str_replace('"','',$split_this);
      $split_this = trim($split_this);
      $split_this = '"' . $split_this . '"';
    

    Give the <p id="ptag1"> tag an id and put a hidden input

     <input type="hidden" name="ptag_value" />
    

    with JavaScript you could set

     document.getElementById('ptag_value').value = document.getElementById('ptag1').innerHTML;
    

    If their server supports fopen

       $handle = fopen("http://www.fundgrube.es/es/perfumes/aramis/aramis.html", "r");
       $contents = stream_get_contents($handle);
       $explode( '<p class="name">', $contents ); // may not work
       echo $contents[0];  // 1, 2, 3 , 4, etc 
    

    or

         strip_tags($contents, '<p>'); // should preserve the p tags 
    

    otherwise use blank ''

         strip_tags($contents, ''); // not entirely predictable but can work
    

    Should just leave all the text without any html. Other examples:

    https://stackoverflow.com/questions/15281124/php-split-explode-stringstrong text