如何在第一个后更换所有问号

So, a lot of my form systems redirect back to the previous page, although, they display a message in the process. The way I display a message is by simply using ?message=messageCode in the URL.

However, if they use the form from a page that already has a query string, it adds a second query string, messing everything up.

Example: if I were to login from the navigation bar, on the URL "mywebsite.com/something?message=1", it would log in, but redirect to "mywebsite.com/something?message=1?message=2"

This results in no message being displayed.

What I am asking here is, how could I change all of the question marks AFTER the first question mark, to and signs?

Example: From: mywebsite.com/page?blah=1?something=2?hi=3 To: mywebsite.com/page?blah=1&something=2&hi=3

I have searched around, as well as tried some methods of my own, but nothing seems to work properly.

Use it like below:-

<?php
   $str = 'mywebsite.com/something?message=1?message=2';
   $pos = strpos($str,'?'); //check the last occurrence of ?
   if ($pos !== false) {
       $str = substr($str,0,$pos+1) . str_replace('?','&',substr($str,$pos+1));// replacement of ? to &
   }
  echo $str;
?>

Output:- https://eval.in/388308

What you should be doing is build a proper URL, appending ? or & when appropriate.

$url = 'mywebsite.com/something?message=1';
$new_url = sprintf('%s%s%s', 
    $url, 
    strpos($url, '?') === false ? '?' : '&',
    http_build_query(['message' => 2])
);

Or, first parse the previous URL and merge the query string.