Monday, September 23, 2019
PHP Parse HTML code
Answer
Answer
Possible Duplicate:
Best methods to parse HTML
How can I parse HTML code held in a PHP variable if it something like:
T1
Lorem ipsum.T2
The quick red fox...T3
... jumps over the lazy brown FROG!
I want to only get the text that's between the headings and I understand that it's not a good idea to use Regular Expressions.
Answer
Use PHP Document Object Model:
$str = 'T1
Lorem ipsum.T2
The quick red fox...T3
... jumps over the lazy brown FROG';
$DOM = new DOMDocument;
$DOM->loadHTML($str);
//get all H1
$items = $DOM->getElementsByTagName('h1');
//display all H1 text
for ($i = 0; $i < $items->length; $i++)
echo $items->item($i)->nodeValue . "
";
?>
This outputs as:
T1
T2
T3
[EDIT]: After OP Clarification:
If you want the content like Lorem ipsum. etc, you can directly use this regex:
$str = 'T1
Lorem ipsum.T2
The quick red fox...T3
... jumps over the lazy brown FROG';
echo preg_replace("#.*?#", "", $str);
?>
this outputs:
Lorem ipsum.The quick red fox...... jumps over the lazy brown FROG
Subscribe to:
Post Comments (Atom)
hard drive - Leaving bad sectors in unformatted partition?
Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...
-
Using Windows 7. When I RDP to a PC I'd like to be able to logout of the session without the screen reverting to a Ctrl+Alt+Del Login sc...
-
I tried adding grubx64.efi in the Windows Boot Manager using BCDEdit. However when I boot up my computer and try to start GRUB from Windows ...
-
So I installed Ubuntu Netbook Remix 9.10 onto my Asus EeePC 1008HA netbook. It worked perfectly and was pretty quick. Restarting, suspending...
No comments:
Post a Comment