Analyze content for a keyword to appear inside h1, h2 and h3 headers

Given the content block, I am looking to create a function in PHP to check for the presence of a keyword or keyword phrase within the h1-h3 header tags ...

For example, if the keyword was “ Blue Violin ” and the block of text was ...

You do not see a lot of blue violins. Most violins have a natural finish. <h1> If you see a blue violin, its really a rarity </ h1>

I want my function to return:

  • Key phrase makes in h1 tag
  • Key phrase not showing in h2 tag
  • Key phrase not showing in h2 tag
0
source share
2

DOM XPath :

/html/body//h1[contains(.,'Blue Violin')]

h1 body, "Blue Violin" , . TextNode, . text(). DOMNodeList.

, , :

$dom = new DOMDocument;
$dom->load('NewFile.xml');
$xPath = new DOMXPath($dom);
echo $xPath->evaluate('count(/html/body//h1[contains(.,"Blue Violin")])');

, XPath. XHTML, loadXML. loadHTML loadHTMLFile. , XPath , . h1, h2 h3, //h1 .

, contains , - - , . , DOM ( libxml) XPath 1.0. , XPath , PHP 5.3 PHP XPath, .

$dom = new DOMDocument;
$dom->load('NewFile.xml');
$xpath = new DOMXPath($dom);
$xpath->registerNamespace("php", "http://php.net/xpath");
$xpath->registerPHPFunctions();
echo $xpath->evaluate('count(/html/body//h1[contains(php:functionString("strtolower", .),"blue violin")])');

, , , contains PHP, .

+2

Source: https://habr.com/ru/post/1773331/


All Articles