note 98975 deleted from function.substr by peehaa

From: Date: Mon, 28 Nov 2016 19:43:34 +0000
Subject: note 98975 deleted from function.substr by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207713@lists.php.net to get a copy of this message
Note Submitter: jeff dot swain at pcmmllc dot com ---- I noticed a slight issue when parsing out long strings using the substr function. Here is my string: $merge = "UPDATE AssistanceRequest SET RequestorID = '4301' WHERE RequestorID IN ( '4535','6222','4865','5137','4893')" To parse out the WHERE portion I used: $whereClause = substr($merge, strpos($merge,'WHERE', (strlen($merge) - strpos($merge,'WHERE')))); Normally the function returned: $whereClause = "WHERE RequestorID IN ( '4535','6222','4865','5137','4893')" This $whereClause gives me the WHERE clause to modify the MSSQL database records being manipulated. So that when I used $whereClause as the WHERE clause to create subsequent SQL, I used the following syntax: $setDeleteFlag = "UPDATE AssistanceRequestor SET bIsDirty = 'DELETE' " . $whereClause; This should have returned: $setDeleteFlag = "UPDATE AssistanceRequestor SET bIsDirty = 'DELETE' WHERE RequestorID IN ( '4535','6222','4865','5137','4893')" As long as the length of the original $merge string was less than 104 characters, the $setDeleteFlag sql came out correctly. However, when the length of the original $merge string exceeded 104 characters, I got this returned: $setDeleteFlag = "UPDATE AssistanceRequestor SET bIsDirty = 'DELETE' UPDATE AssistanceRequestor SET bIsDirty = 'DELETE' WHERE RequestorID IN ( '4535','6222','4865','5137','4893')" The result was that the bIsDirty field for every record in the database was set to 'DELETE'. I fixed it by breaking apart the substr to create the original $whereClause so that it looked like this: $wherePosition = strpos($merge,'WHERE'); $whereClause = substr($merge, $wherePosition, strlen($merge) - $wherePosition); $setDeleteFlag = "UPDATE AssistanceRequestor SET bIsDirty = 'DELETE' " . $whereClause; I do have to note that I run PHP 5.x on my development server, while I think the production host is still on 4.x. I did not seem to have an issue in development, but I don't think I tested strings longer than 104 characters. Maybe this issue has been corrected in version 5.x.

« previous php.notes (#207713) next »