First, let’s review the basic usage of the substr() function. The substr() function returns a portion of a string starting from a specified position and of a specified length. Its syntax is as follows:
<span><span><span class="hljs-title function_ invoke__">substr</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$string</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$start</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$length</span></span><span> = </span><span><span class="hljs-literal">NULL</span></span><span>): </span><span><span class="hljs-keyword">string</span></span><span>
</span></span>
$string: The string to be processed.
$start: Starting position. A negative number means counting from the end of the string.
$length: Optional. Specifies the length of the substring. If omitted, it returns from $start to the end of the string.
JSON (JavaScript Object Notation) is a lightweight data interchange format commonly used for frontend-backend data exchange. A typical JSON structure looks like this:
<span><span><span class="hljs-punctuation">{</span></span><span>
</span><span><span class="hljs-attr">"name"</span></span><span><span class="hljs-punctuation">:</span></span><span> </span><span><span class="hljs-string">"John"</span></span><span><span class="hljs-punctuation">,</span></span><span>
</span><span><span class="hljs-attr">"age"</span></span><span><span class="hljs-punctuation">:</span></span><span> </span><span><span class="hljs-number">30</span></span><span><span class="hljs-punctuation">,</span></span><span>
</span><span><span class="hljs-attr">"city"</span></span><span><span class="hljs-punctuation">:</span></span><span> </span><span><span class="hljs-string">"New York"</span></span><span>
</span><span><span class="hljs-punctuation">}</span></span><span>
</span></span>
JSON data generally consists of key-value pairs, where the keys are strings, and the values can be strings, numbers, booleans, arrays, or even nested objects. When extracting a value from a JSON string, we typically use the json_decode() function to parse it into a PHP object or associative array, then access the key directly.
Let’s assume we have the following JSON string:
<span><span><span class="hljs-variable">$json_data</span></span><span> = </span><span><span class="hljs-string">'{"name": "John", "age": 30, "city": "New York"}'</span></span><span>
</span></span>
If we want to extract the portion "name": "John", theoretically, we can use substr() to get part of the string. For example:
<span><span><span class="hljs-variable">$substring</span></span><span> = </span><span><span class="hljs-title function_ invoke__">substr</span></span><span>(</span><span><span class="hljs-variable">$json_data</span></span><span>, </span><span><span class="hljs-number">0</span></span><span>, </span><span><span class="hljs-number">20</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$substring</span></span><span>; </span><span><span class="hljs-comment">// Output: {"name": "John"}</span></span><span>
</span></span>
This method does work to extract part of the JSON string, but there are issues. JSON is a structured format, and substr() operates purely on character positions. It doesn’t understand the hierarchy or relationships in JSON. You have to manually calculate the positions, which is highly unreliable for complex or dynamically changing JSON structures.
Lacks structural awareness: substr() works on a byte level and can’t parse the actual structure of JSON. Keys and values may contain special characters or escape sequences that substr() can’t handle properly.
Inflexibility: If the JSON structure changes, recalculating the start position and length becomes tedious and error-prone.
No error handling: substr() doesn’t check if the extracted string is valid JSON, which can lead to parsing errors.
The proper way to handle JSON in PHP is to use json_decode() to parse the JSON string into a PHP variable. This allows you to access fields directly. For example:
<span><span><span class="hljs-variable">$json_data</span></span><span> = </span><span><span class="hljs-string">'{"name": "John", "age": 30, "city": "New York"}'</span></span><span>
</span><span><span class="hljs-variable">$data</span></span><span> = </span><span><span class="hljs-title function_ invoke__">json_decode</span></span><span>(</span><span><span class="hljs-variable">$json_data</span></span><span>, </span><span><span class="hljs-literal">true</span></span><span>); </span><span><span class="hljs-comment">// Convert to associative array</span></span><span>
<p></span>echo $data['name']; // Output: John<br>
</span>
If you only need part of the JSON, simply access the desired key. There’s no need for substr(), which makes your code safer and more maintainable.
Although the substr() function can technically be used to extract part of a JSON string in some cases, it's not the ideal tool for handling JSON. Using substr() makes the code fragile and error-prone, especially when the JSON structure changes. The recommended approach is to use json_decode() to parse the JSON string and access the required data via arrays or objects.
For tasks involving manipulation of JSON strings, it’s best to use json_decode() in combination with json_encode() to safely and reliably extract or modify data.