0

I have a section of html code like this :

      <a>
             <img scr=""/>some text here...
      </a>

      <a>
             <img scr=""/>some text here...
      </a>

I need to get the some text here...

i was trying like this : let say the section above is in a html dom.$html

foreach ($html->find('a') as $myText)
{
      echo '-----PPPP---->>>>'.$myText->plaintext.'this is test<br/>';   
}

But it's printing the text and image both.I just need the text

2
  • Odd. ->plaintext should only retrieve text nodes from the node you're dealing with. ->innertext would return child nodes as well as the text. Commented Feb 22, 2012 at 15:00
  • 1
    Simple HTML DOM Parser has it's issues, probably that's one of them. You could change to a better parser like DOMDocument and then just use an xpath expression to obtain the text. Commented Feb 22, 2012 at 15:02

4 Answers 4

1
->find('a')->plaintext;

should do it

Sign up to request clarification or add additional context in comments.

Comments

0

I didn't use Simple HTML DOM Parser but instead went with DomDocument:

$html = <<<HTML
<a><img src=""/>some text here...</a>
<a><img scr=""/>some text here...</a>
HTML;

$dom = new DomDocument();
$dom->loadHTML($html);
$links = $dom->getElementsByTagName("a");
foreach ($links as $link) {
    var_dump($link->textContent);
}

results in:

string 'some text here...' (length=17)
string 'some text here...' (length=17)

Comments

0

How about splitting it into two parts and printing after image tag:

foreach ($html->find('a') as $myText)
{
   $parts = explode("/>", $myText);
   echo $parts[1];
}

Comments

0

I think you should use DOMDocument in PHP.

Like this:

$dom = new DOMDocument();
$dom->load($htmlfile);
$atags = $dom->getElementsByTagName('a');
foreach ($atags as $atag) {
    echo $atag->textContent;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.