how to find hello
keyword in a html content inside <p>
tag directly (not sub tags e.g. <a>
tag) and replace it with a link
i dont want alter the html attributes
HTML:
<p>hello world
<a href="hello.html">hello</a>
</p>
<img src="hello.png" alt="image" />
I need an output like this:
<p><a href="hello">hello</a> world
<a href="hello.html">hello</a>
</p>
<img src="hello.png" alt="image" />
not this:
<p><a href="hello">hello</a> world
<a href="<a href="hello">hello</a>.html">hello</a>
</p>
<img src="<a href="hello">hello</a>.png" alt="image" />
What i tried
include('inc/simple_html_dom.php');
// Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$dom = '
<p>hello world
<a href="hello.html">hello</a>
</p>
<img src="hello.png" alt="image" />
';
$html->load($dom);
foreach($html->find('p') as $p)
{
$p->innertext = str_replace($searchArray, $replaceArray, $p->innertext);
}
echo $html;