Current Location: Home> Latest Articles> Want to Check If a String Starts with a Certain Character? Try PHP’s substr Function!

Want to Check If a String Starts with a Certain Character? Try PHP’s substr Function!

gitbox 2025-08-24
<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// This part is not related to the main article, you can place some initialization code or comments here</span></span><span>
</span><span><span class="hljs-comment">// For example, set the timezone</span></span><span>
</span><span><span class="hljs-title function_ invoke__">date_default_timezone_set</span></span><span>(</span><span><span class="hljs-string">&#039;Asia/Shanghai&#039;</span></span><span>);
</span><span><span class="hljs-meta">?&gt;</span></span><span>
<p><hr></p>
<p></span><?php<br>
/*<br>
Title: Want to Check If a String Starts with a Certain Character? Try PHP’s substr Function!</p>
<p>During development, it’s common to need to determine whether a string starts with a specific character or substring. PHP offers several ways to achieve this, and using the substr function is a simple and efficient solution.</p>
<ol>
<li>
<p>Introduction to the substr Function<br>
The substr() function is used to return part of a string. Its basic usage is:<br>
string substr ( string $string , int $start [, int $length ] )</p>
</li>
</ol>
<ul>
<li>
<p>$string: The input string.</p>
</li>
<li>
<p>$start: The starting position (beginning from 0).</p>
</li>
<li>
<p>$length: Optional, the length of the returned substring.</p>
</li>
</ul>
<ol start="2">
<li>
<p>Using substr to Check String Prefix</p>
</li>
</ol>
<p>Suppose we want to check whether a string $str starts with a substring $prefix. We can use substr to extract the beginning of the string and compare it with $prefix:</p>
<p>Here’s an example:<br>
*/</p>
<p>function startsWith($str, $prefix) {<br>
// Get the length of $prefix<br>
$len = strlen($prefix);<br>
// Extract the first $len characters of the string<br>
$start = substr($str, 0, $len);<br>
// Check if they are equal<br>
return $start === $prefix;<br>
}</p>
<p>// Test examples<br>
$str = "Hello, world!";<br>
$prefix1 = "Hello";<br>
$prefix2 = "world";</p>
<p>echo startsWith($str, $prefix1) ? "'$str' starts with '$prefix1'\n" : "'$str' does not start with '$prefix1'\n";<br>
echo startsWith($str, $prefix2) ? "'$str' starts with '$prefix2'\n" : "'$str' does not start with '$prefix2'\n";</p>
<p>/*<br>
Output:<br>
'Hello, world!' starts with 'Hello'<br>
'Hello, world!' does not start with 'world'</p>
<ol start="3">
<li>
<p>Notes</p>
</li>
<li>
<p>When using substr with Chinese strings, incomplete character cuts may occur since each Chinese character occupies multiple bytes. For handling Chinese strings, it’s better to use mb_substr.</p>
</li>
<li>
<p>If you need case-insensitive checking, convert both the string and the prefix to the same case (e.g., strtolower).</p>
</li>
<li>
<p>Conclusion<br>
Using the substr function to check if a string starts with a particular character or substring is concise and easy to understand. It’s a common solution among PHP developers. Of course, depending on your requirements and character encoding, you might also consider alternatives such as strpos or mb_substr.</p>
</li>
</ol>
<p data-is-last-node="" data-is-only-node="">Hopefully, this article helps you better understand and apply PHP’s substr function!<br>
*/<br>
</span>