Hello. There is a script that finds the necessary lines, after it must find each matching line in the text, and if found, replace it with another text:

Sketched this code:

foreach ($a_links2 as & $value2) { echo $value2->src; //строки которые нужно заменить $text = str_replace($value2->src, "КАРТИНКА", $article['content']); } echo $text; 

The result is that it replaces only the last line found.

    2 answers 2

    Try the following option, it should work correctly:

     $text = $article['content']; foreach ($a_links2 as & $value2) { $text = str_replace($value2->src, "КАРТИНКА", $text); } echo $text; 
    • so I do not need to display the same text several times. There is an article in which some words need to be replaced, these words are searched and replaced. and withdraw all you need once. -
    • @ Denis try a new version - Yaroslav Molchan
    • the result is the same ... -
    • Are you sure that you have not 1 match? I checked the code for myself, entered random data and everything worked, check that you have in the variables $ value2-> src and $ text, there can really be 1 coincidence - Yaroslav Molchan
    • checked several times. coincidence is not one. if you do as you suggested initially, it replaces everything that is needed, but the article displays several times, and each time with one replacement (the first line is replaced immediately, then the second is replaced, while the first is not replaced). etc. -

    That's right, you specify each iteration of $ text = (Assign) and not add to the existing variable.

      $text .= str_replace($value2->src, "КАРТИНКА", $article['content']); 
    • replaces now only the first line - iKey