1

I use the PHP simple html dom parser library and I want to replace only all 'manteau' word by [WORD FIND HERE]. This is my code below which doesn't work with words which are not in tags. It only works with the word 'manteau' within the strong tag. How to parse all nodes texts?

Note : str_replace is not a solution. DOM PARSER need to be used here. I don't want to select the word in anchor or image tags.

<?php

    require_once '../simple_html_dom.php';
    $html = new simple_html_dom();
    $html = str_get_html('Un manteau permet de tenir chaud. Ce n\'est pas 
    un porte-manteau. Venez découvrir le <a href="pages/manteau">nouveau 
    manteau</a> du porte-manteau. 
    <h1>Tout savoir sur le Manteau</h1>  
    <p>
        Le <strong>manteau</strong> est un élèment important à ne pas négliger. 
        Pas comme le porte-manteau. 
    </p>
    <img src="path-to-images-manteau" title="Le manteau est beau">');


    $nodes = $html->find('*');

    foreach($nodes as $node) {
        if(strpos($node->innertext, 'manteau') !== false) {
            if($node->tag != 'a')
              $node->innertext = '[WORD FIND HERE]';
            }
        }
    }

    echo $html->outertext;

?>
4
  • 2
    Parsing sounds a bit like a overkill for just replacing words here. Why not just using str_replace Commented Sep 27, 2017 at 11:06
  • dom manipulation need to be used here. I don't want to select the word in anchor or image tag Commented Sep 27, 2017 at 11:30
  • I understant. Then it is a good idea to use a parse. I think you also could use Regex for this. (*s/a overkill/an overkill/) Commented Sep 27, 2017 at 12:37
  • @MaximeDeuton If any of the provided solutions worked please be sure to accept it. Commented Oct 23, 2017 at 8:21

2 Answers 2

1

Use str_replace(); instead! http://php.net/manual/en/function.str-replace.php

$html = str_replace('manteau', 'rubber chicken', $html);
echo $html;

See it working here; https://3v4l.org/GRfji

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer but str_replace is not a solution. dom manipulation need to be used here. I don't want to select the word in anchor or image tag.
0

Maybe it is an option to exclude the tags that you do not want to change.

For example:

<?php
require_once '../simple_html_dom.php';
$html = new simple_html_dom();
$html = str_get_html('Un manteau permet de tenir chaud. Ce n\'est pas 
un porte-manteau. Venez découvrir le <a href="pages/manteau">nouveau 
manteau</a> du porte-manteau. 
<h1>Tout savoir sur le Manteau</h1>  
<p>
    Le <strong>manteau</strong> est un élèment important à ne pas négliger. 
    Pas comme le porte-manteau. 
</p>
<img src="path-to-images-manteau" title="Le manteau est beau">');


$nodes = $html->find('*');

$tagsToExclude = [
    "a",
    "img"
];

foreach($nodes as $node) {
    if (!in_array($node->tag, $tagsToExclude)) {
        if(strpos($node->innertext, 'manteau') !== false) {
            $node->innertext = str_replace("manteau", '[WORD FIND HERE]', $node->innertext);
        }
    }
}

echo $html->outertext;
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.