Home > database >  How do I replace a specific position (X,Y) in a string with multiple lines? (PHP)
How do I replace a specific position (X,Y) in a string with multiple lines? (PHP)

Time:11-07

I've got a field 30x7 (see below) and I want to replace a specific position.
The position is randomly given and I want to replace the random position (X,Y) with a letter.
I already got the field and the replacement but it's not exactly what I want.

`

 $field=('################################')."\n".str_repeat.('#                              #\n', 7)('################################')."\n"

`
The field will look like this

##############################   
#                            #  
#                            #  
#                            #  
#                            #  
#                            #  
#                            #  
#                            #  
##############################  

and the empty space at the given position should be replaced with a letter.
It's 32x9 with the hashtags so only the empty spaces are the actual field.
I don't know how to exclude the surrounding hashtags from the position and how to calculate the actual position from x and y.

   $position=($position_x)*($position_y);
   $field=substr_replace($field,'O',$position,1);
   echo("$field");

That's what I tried. I know that $position is wrong but I don't know how to fix it.
Is it even possible with the way I created the field or should I try it differently?
Thanks for your help in advance!

CodePudding user response:

You need to make sure your $field is correct. Your code has some syntax errors.

The trick is to calculate the coordinates.

  • increase x 1 for the left border
  • increase y 1 for the top border
  • the cols are 3, because of left border, right border and the newline
  • For the final position multiply the number of rows by number of columns plus x
$placeField = function (string $char, int $x, int $y): string {
    $cols = 30;
    $rows = 7;

    $topBottomFrame = str_repeat('#', $cols   2) . "\n";
    $centerFrame    = '#' . str_repeat(' ', $cols) . "#\n";
    $field          = $topBottomFrame . str_repeat($centerFrame, $rows) . $topBottomFrame;

    if ($x < 0 || $x >= $cols || $y < 0 || $y >= $rows) {
        return "Invalid coordinates\n";
    }

    $x  ;
    $y  ;

    return substr_replace($field, $char, $y * ($cols   3)   $x, 1);
};

echo $placeField("A", 0, 0);
echo $placeField("B", 29, 6);
echo $placeField("C", 15, 3);

Output

################################
#A                             #
#                              #
#                              #
#                              #
#                              #
#                              #
#                              #
################################
################################
#                              #
#                              #
#                              #
#                              #
#                              #
#                              #
#                             B#
################################
################################
#                              #
#                              #
#                              #
#               C              #
#                              #
#                              #
#                              #
################################
  • Related