Search code examples
phpecho

run additional php code in echo after replacing strings


i want to execute addition php code after replacing some string from form

here os my form code on form.php

<form method="post" action="result.php">
<input type="text" name="yourtext">
<input type="submit">
</form>

and for example the text is

my name is bagu and i live under a rock thanks

here the result.php code


<?php

$mytext = $_POST['yourtext'];

$find = ["my name is","and i live", "thanks"];

$replace = ["<?php open_form('Form/insert') ?><input name='name' type='text' value='","'><br><input name='address' type='text' value='","'><input type='submit'><?php form_close() ?>"];

$intoform = str_replace($find, $replace, $mytext);

echo $intoform;

?>

the result i want is to the "bagu" and "under a rock" each in a textbox which is working

but the trouble is the form php code in echo is not working (i think??? because it is)

and yes this is form for codeigniter

thanks

======

edit: i have tried the result.php without echo and variable aka the result i wanted after i submit text in form.php

<?php open_form('Form/insert') ?>
<input name='name' type='text' value='bagu'><br>
<input name='address' type='text' value='under a rock'>
<input type='submit'>
<?php form_close() ?>

and it will work if "<?php" changed to "<?= " idk if this helps tho

=====

edit 2:

aaand if i input

my name is bagu and i live under a rock thanks my name is ariel and i live under the sea thanks my name is snow white and i live under a pumpkin thanks 

i will have 3 forms


Solution

    1. Extract the name and address using preg_match. The die clause is just used to indicate input string formatting errors and can be replaced with more friendly prompts as needed.

      preg_match('/my name is (.+) and i live (.+) thanks/', $mytext, $m)
      or die('Input text is invalid');
      
    2. If preg_match succeeds, $m[1] will contain the name, and $m[2] will contain the address, then you can insert the values into the form

      <?php open_form('Form/insert') ?>
      <input name='name' type='text' value='<?=$m[1]?>'><br>
      <input name='address' type='text' value='<?=$m[2]?>'>
      <input type='submit'>
      <?php form_close() ?>
      
    • It should be added that in actual practice, the legality of the name and address entered by the user should be checked. For example to restrict them to only contain letters and spaces, the regex (.+) in the first step can be changed to ([A-Za-z ]+).

    To match the text multiple times, use preg_match_all instead of preg_match

    preg_match_all('/my name is (.+?) and i live (.+?) thanks/', $mytext, $m)
    or die('Input text is invalid');
    

    Each matched item will now contain multiple values, then you can wrap the original output in foreach to output multiple forms

    <?php foreach($m[0] as $i => $_):
    open_form('Form/insert') ?>
    <input name='name' type='text' value='<?=$m[1][$i]?>'><br>
    <input name='address' type='text' value='<?=$m[2][$i]?>'>
    <input type='submit'>
    <?php form_close()
    endforeach; ?>