Current Location: Home> Latest Articles> How to Use substr in PHP to Extract Specific Content from a String Element in an Array?

How to Use substr in PHP to Extract Specific Content from a String Element in an Array?

gitbox 2025-06-22

In PHP, the substr function is a commonly used string manipulation function that extracts part of a string. When we need to extract a specific segment from a string element within an array, substr proves very useful. This article will demonstrate, through an example, how to use the substr function to extract specified content from a string element inside an array.

What is the substr Function?

The substr function returns a portion of a string. Its basic 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>
  • $string: The input original string.

  • $start: The starting position of extraction, which can be negative to indicate counting from the end of the string.

  • $length: The length of the substring to extract (optional). If omitted, it extracts until the end of the string. If negative, it counts backward from the end.

How to Use substr with Arrays?

Suppose we have an array where each element is a string, and we want to extract a part of a specific element. We can achieve this by following these steps.

Example: Extracting a Specific Part from an Element in an Array

Assume we have the following array where each element contains a date-time string, and we want to extract the date portion from each element.

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Define an array containing date-time strings</span></span>
</span><span><span class="hljs-variable">$dateArray</span></span> = [
    </span><span><span class="hljs-string">&#039;2025-06-18 14:30:00&#039;</span></span>,
    </span><span><span class="hljs-string">&#039;2023-09-12 09:45:00&#039;</span></span>,
    </span><span><span class="hljs-string">&#039;2021-12-25 17:00:00&#039;</span></span>
];
</span>
</span><span><span class="hljs-comment">// Loop through the array and use substr to extract the date part</span>
</span><span><span class="hljs-keyword">foreach</span> (<span class="hljs-variable">$dateArray</span> as <span class="hljs-variable">$date</span>) {
    </span><span><span class="hljs-comment">// Extract the first 10 characters, i.e., the "YYYY-MM-DD" part</span>
    </span><span><span class="hljs-variable">$datePart</span> = <span class="hljs-title function_ invoke__">substr</span>(<span class="hljs-variable">$date</span>, 0, 10);
    </span><span><span class="hljs-keyword">echo</span> "Date part: <span class="hljs-subst">$datePart</span>\n";
}
</span><span><span class="hljs-meta">?></span></span>
</span>

Output:

<span><span><span class="hljs-section">Date part: 2025-06-18</span></span>
</span><span><span class="hljs-section">Date part: 2023-09-12</span></span>
</span><span><span class="hljs-section">Date part: 2021-12-25</span></span>
</span>

In this example, we use substr($date, 0, 10) to extract the first 10 characters from each date-time string, which corresponds to the date in the “YYYY-MM-DD” format.

Further Applications

Extracting a Substring from a Specific Position

If we want to extract content from a specific position in the string, substr can be used to target that position. For example, if we want to extract the time part (“HH:MM:SS”) from the date-time string, we can adjust the substr parameters as follows:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-keyword">foreach</span> (<span class="hljs-variable">$dateArray</span> as <span class="hljs-variable">$date</span>) {
    </span><span><span class="hljs-comment">// Extract 8 characters starting from the 11th character, i.e., the time part "HH:MM:SS"</span>
    </span><span><span class="hljs-variable">$timePart</span> = <span class="hljs-title function_ invoke__">substr</span>(<span class="hljs-variable">$date</span>, 11, 8);
    </span><span><span class="hljs-keyword">echo</span> "Time part: <span class="hljs-subst">$timePart</span>\n";
}
</span><span><span class="hljs-meta">?></span></span>
</span>

Output:

<span><span><span class="hljs-section">Time part: 14:30:00</span></span>
</span><span><span class="hljs-section">Time part: 09:45:00</span></span>
</span><span><span class="hljs-section">Time part: 17:00:00</span></span>
</span>

Using Negative Indexes

The substr function supports negative values for the $start parameter, where a negative number counts from the end of the string. For example, substr($string, -5) returns the last 5 characters of the string.

Suppose we have an array of URLs, and we want to extract the file extension (such as .php, .html, etc.). We can do this using a negative index as shown below:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$urls</span> = [
    </span><span><span class="hljs-string">'https://example.com/index.php'</span>,
    </span><span><span class="hljs-string">'https://example.com/about.html'</span>,
    </span><span><span class="hljs-string">'https://example.com/contact.php'</span>
];
</span>
</span><span><span class="hljs-keyword">foreach</span> (<span class="hljs-variable">$urls</span> as <span class="hljs-variable">$url</span>) {
    </span><span><span class="hljs-comment">// Extract the last 4 characters from the string to get the file extension</span>
    </span><span><span class="hljs-variable">$extension</span> = <span class="hljs-title function_ invoke__">substr</span>(<span class="hljs-variable">$url</span>, -4);
    </span><span><span class="hljs-keyword">echo</span> "File extension: <span class="hljs-subst">$extension</span>\n";
}
</span><span><span class="hljs-meta">?></span></span>
</span>

Output:

<span><span><span class="hljs-section">File extension: .php</span></span>
</span><span><span class="hljs-section">File extension: .html</span></span>
</span><span><span class="hljs-section">File extension: .php</span></span>
</span>

Summary

The substr function provides PHP with powerful substring extraction capabilities. Whether extracting specific parts from string elements within an array or using negative indexes to extract content from the end of strings, substr efficiently accomplishes these tasks. Depending on your needs, you can flexibly adjust the $start and $length parameters to precisely extract the required substring.