Note that there are some explanatory texts on larger screens.

plurals
  1. POphp remove everything after a bracket
    primarykey
    data
    text
    <p>I'm trying to save a string and the data is coming in with stuff that I don't want at the end of an array.</p> <pre><code>["bunch", "of", "stuff"]unwantedtext </code></pre> <p>I'm struggling with getting rid of everything after the closing bracket. This is my latest attempt. I end up with an empty file.</p> <pre><code>$toPost = substr($str, 0, strrpos($str, '\]')); </code></pre> <p>Perhaps my escape isn't working correctly? I've also tried doublequotes.</p> <p>Here's an update to the original question.</p> <p>This is my Javascript function:</p> <pre><code>goalChange = function() { var i, key, len, postThis; key = void 0; postThis = {}; i = 0; len = localStorage.length; while (i &lt; len) { key = localStorage.key(i); postThis[key] = "" + localStorage.getItem(key); i++; } postThis.goals = localStorage.getItem("goals"); postThis.userid = username; return $.ajax({ url: "goalChange.php", type: "POST", data: postThis, success: function(response, textStatus, jqXHR) { return console.log("Yay, the save worked!"); }, error: function(jqXHR, textStatus, errorThrown) { return console.log("Didn't work so good..."); } }); }; </code></pre> <p>Here's the PHP file:</p> <pre><code>&lt;? if ($_POST) { $user = $_POST['userid']; // ADDED THIS LINE AND CHANGED NEXT TO GET STUFF INTO THE USERS OWN FILE $logFile = dirname($_SERVER['SCRIPT_FILENAME']) . "/" . $user . ".json"; if (!file_exists($logFile)) { touch($logFile); chmod($logFile, 0777); } $newStr = ($_POST); $str = str_replace('\\', '', $newStr); $toPost = substr($str, 0, strrpos($str, ']')); file_put_contents($logFile, $toPost); echo "OK"; return; } </code></pre> <p>I need to use the userid to save the file with the correct name. That's working, but the file is getting the userid added to the end of the array that I want to keep.</p> <p>Thanks for the help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload