skip to Main Content

This is a problem related to adding a counter when renaming a value with the same name.

I have a form with a textarea where you can write text inside. Inside the textarea, the writer can write the word foto which must follow a particular path when sending the form. I specify that the written text will end up in a txt file.

I created a code that replaces the word *foto* with "<img src="img/foto$y.jpg"> assuming that each word *foto* is replaced in "<img src="img/foto1. jpg">, "<img src="img/foto2.jpg"> in the txt file after submitting the form…

Instead I find that the file is generated with n.2 "<img src="img/foto1.jpg"> without increasing the counter (1-2-3 etc… based on the number of photos).

In the code $vfoto is the number of photos the user has decided to upload. $v5 is the article.

Html:

<form>
<textarea name="post_content" id="article"</textarea>
</form>

Text write Textarea box:

    <p>Test</p>
    <p>*foto*</p>
    <p>*foto*</p>

PHP:

$v5 = $_POST["post_content"];


if ($vfoto > 0){
    $y = 1;
    for ($x = 1; $x <= $vfoto; $x++) {
     $v5 = str_ireplace("<p>*foto*</p>","<img src="img/foto$y.jpg" 
     class="img_article_box">",$v5);
     $y++;
    }
    $myfile = fopen("provafile.txt","w") or die("Unable to open file!");
    fwrite($myfile, html_entity_decode($v5));
    fclose($myfile);
}

The result I would expect is:

<p>test</p>
<img src="img/foto1.jpg" class="img_article_box">
<img src="img/foto2.jpg" class="img_article_box">

2

Answers


  1. simple do this, use regex and the prebuild php function to handle replacements,

    //Your input
    $text = $_POST["post_content"];
    
    // Regex Pattern
    $pattern = '/*foto*/';
    
    // Index counter
    $replacementIndex = 1;
    
    // Replaced Text
    $replacedText = preg_replace_callback($pattern, function ($matches) use (&$replacementIndex) {
        $replacement = "<img src="img/foto" . $replacementIndex . ".jpg" class="img_article_box">";
        $replacementIndex++;
        return $replacement;
    }, $text);
    
    // Result after replacements
    echo $replacedText;
    

    Input

    <p>Test</p>
    <p>*foto*</p>
    <p>*foto*</p>
    

    Output

    <p>Test</p>
    <p><img src="img/foto1.jpg" class="img_article_box"></p>
    <p><img src="img/foto2.jpg" class="img_article_box"></p>
    
    Login or Signup to reply.
  2. You can use the preg_replace function with the limit argument (fourth parameter) to replace just one instance of the text. This would allow you to replace the next instance in the text.

    <?php
    if( isset($_POST['post_content']) ){
        $v5 = $_POST['post_content'];
    
        preg_match_all("@*foto*@s", $v5, $foto_matches);
    
        if( $foto_matches[0] ){
            foreach($foto_matches[0] as $k=>$foto_match){
                $find = preg_quote($foto_match);
    
                $replacement = '<img src="img/foto'.($k+1).'.jpg" class="img_article_box">';
                
                $v5 = preg_replace("@".$find."@", $replacement, $v5, 1);
            }
        }
    
        $myfile = fopen("provafile.txt","w") or die("Unable to open file!");
        
        fwrite($myfile, html_entity_decode($v5));
        
        fclose($myfile);
    }?>
    
    <form method="post">
    <textarea name="post_content" id="article"></textarea>
    <input type="submit">
    </form>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search