0
votes

I'm working on a project. It searches and replaces words from the database in a given text. In the database a have 10k words and replacements. So ı want to search for each word and replace this word's replacement word.

By the way, I'm using laravel. I need only replacement ideas.

I have tried some ways but it replaces only one word.

My database table structure like below;

id word replacement

1 test testing

etc

The text is coming from the input and after the replacement, I wanna show which words are replaced in a different bg color in the result page.

I tried below codes working fine but it only replaces one word.

    $article = trim(strip_tags($request->article));
    $clean = preg_split('/[\s]+/', $article);
    $word_count = count($clean);
    $words_from_database_for_search = Words::all();
    foreach($words_from_database_for_search as $word){
        $content = str_replace($word['word'],
            "<span class=\"badge badge-success\">$word[replacement] 
    </span>",
            $article);
    }
    $new_content = $content ;
    $new_content_clean = preg_split('/[\s]+/', $new_content);
    $new_content_word_count= count($new_content_clean);

Edit,

Im using preg_replace instead of str_replace. I get it worked but this time i wanna show how many words changed so i tried to find the number of changed words from the text after replacement. It counts wrong.

Example if there is 6 changes it show 3 or 4

It can be done via preg_replace_callback but i didnt use it before so i dont know how to figure out; My working codes are below;

    $old_article = trim(strip_tags($request->article));
    $old_article_word_count = count($old_article );
    $words_from_database_array= Words::all();
    $article_will_replace = trim(strip_tags($request->article));
    $count_the_replaced_words = 0;
    foreach($words_from_database_array as $word){

        $article_will_replace = preg_replace('/[^a-zA- 
    ZğüşıöçĞÜŞİÖÇ]\b'.$word['word'].'\b\s/u',
            " <b>".$word['spin']."</b> ",
            $article_will_replace );

        $count_the_replaced_words = preg_match_all('/[^a-zA- 
ZğüşıöçĞÜŞİÖÇ]\b'.strip_tags($word['spin']).'\b\s/u',$article_will_replace 
     );
        if($count_the_replaced_words ){

            $count_the_replaced_words ++;

        }



    }
2
the main issue is that you're only storing the replacement into $content, but continuing to replace from $article, so only the last replacement appears to work. So either put $content in second param to str_replace, or assign the response to $article and use that. - gingerCodeNinja
So either put $content in second param to str_replace wait what ? - Frankich
gingerCodeNinja sorry bro ı dont understand what u mean? this is the picture what i want i63.tinypic.com/29ngemt.png - sadri
he meant that you are actually storing a new value to $content each time you pass throught the loop so when your loop is finished you have changed only the last word - Frankich
what must i do at this time? MacBooc - sadri

2 Answers

0
votes

Im confused, don't you need the <?php ... ?> around the $word[replacement] ?

    foreach($words_from_database_for_search as $word){
        $content = str_replace($word['word'],
            "<span class=\"badge badge-success\"><?PHP $word[replacement] ?> 
    </span>",
            $article);
    }

and then move this in to the for-loop and add a dot before the equal-sign:

$new_content .= $content ;
0
votes

As others have suggested in comments, it seems your value of $content is being overwritten on each run of the foreach loop, with older iterations being ignored. That's because the third argument of your str_replace is $article, the original, unmodified text. Only the last word, therefore, is showing up on the result.

The simplest way to fix this would be to declare $content before the foreach loop, and then make $content the third argument of the foreach loop so that it is continually replaced with a new word on each iteration, like so:

$content = $article;
foreach($words_from_database_for_search as $word){
    $content = str_replace($word['word'],
        "<span class=\"badge badge-success\">$word[replacement]</span>",
        $content);
}