'Php equivalent needed for python snippet with Xpath custom extension
I am stuck with porting a huge python code to php 8
It's using xpath custom functions inside.
Need help with below code explanation, and even better if someone can give a PHP equivalent.
Common initialization.
import html5lib
from html5lib import HTMLParser
from lxml import etree, html, cssselect
def _html5lib_parser() -> HTMLParser:
return HTMLParser(
html5lib.treebuilders.getTreeBuilder("lxml"),
namespaceHTMLElements=False
)
html_message=html5parser.document_fromstring(s,parser=_html5lib_parser())
Snippet 1: Xpath with user defined function
def text_content(context):
"""XPath Extension to return text content of a node"""
return context.context_node.xpath("string()").strip()
block = html_message.xpath(
("//*[starts-with(mg:text_content(), 'From:')]|"
"//*[starts-with(mg:text_content(), 'Date:')]"))
if block:
block = block[-1]
parent_div = None
Snippet 2: Xpath with another user defined function
def tail(context):
"""XPath Extension function to return tail text of a node"""
return context.context_node.tail or ''
block = html_message.xpath(
("//*[starts-with(mg:tail(), 'From:')]|"
"//*[starts-with(mg:tail(), 'Date:')]"))
if block:
block = block[0]
I am using DomDocument with Xpath till now for rest of the code in Php
$html_message = new DOMDocument();
$html_message->loadHTML($html_string);
$finder = new DomXPath($html_message);
$node = $finder->query();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
