1

i have a string which is being generated dynamically

    <p style="line-height:22px;"><b>Abstract</b><br>
Aims: The purpose of this study was to investigate a possible role of serum LDH as a predictor of cancer.
</p>

the text in this string is dynamic. i just want to extract the text from this string using explode function. my code so far. But it just gives me blank result. no errors no text. I am new to php.

here is the link of the page i am working on.

http://test.pjnmed.com/?mno=284644

I want the text below the abstract.

$content = file_get_contents("https://www.ejmanager.com/index_myjournal.php?jid=" . $jid . "&mno=" . $_GET[mno]);

$abstract_text = explode('<p style="line-height:22px;"><b>Abstract</b><br>', $content);
$content = $abstract_text[1];

$abstract_text = explode('</p>', $content);
$content = $abstract_text[0];

echo $content;
its_zbari
  • 21
  • 1
  • 7

1 Answers1

1

Don't use regexes for parsing HTML

$previous_value = libxml_use_internal_errors(TRUE);

$string ='<p style="line-height:22px;"><b>Abstract</b><br>
Aims: The purpose of this study was to investigate a possible role of serum LDH as a predictor of cancer.
</p>';
$dom = new DOMDocument();
$dom->loadHTML($string);
$p = $dom->getElementsByTagName('p')->item(0);
$p->removeChild($p->getElementsByTagName('b')->item(0));
echo $p->textContent;

libxml_clear_errors();
libxml_use_internal_errors($previous_value);

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • does the result guarantee that the data required is always the first `p`? also, need to ignore `Abstract` – Joseph D. Mar 27 '18 at 01:31
  • @codekaizer I only have the snippet they posted so my answer is limited to their example. But it should point them in the right direction if their HTML is more complicated than that. I also didn't see they did not want "Abstract" in their output so I updated my answer to reflect it. – John Conde Mar 27 '18 at 01:36
  • Actually, i think you are not getting my question. i only have this link to fetch data https://www.ejmanager.com/index_myjournal.php?jid=" . $jid . "&mno=" . $_GET[mno]) everything is generated dynamically i don't know from where. I inspected that string from dev tools. and just want that text. – its_zbari Mar 27 '18 at 01:38
  • This code shows you how to parse the HTML returned from that URL so you can get the text you are looking for. It made need to be tweaked a bit but that's the core code you'll need. If that's not what you are looking you need to edit your question and clarify what you are looking for. – John Conde Mar 27 '18 at 01:43