<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>php substr | ServerAvatar</title>
	<atom:link href="https://serveravatar.com/tag/php-substr/feed/" rel="self" type="application/rss+xml" />
	<link>https://serveravatar.com</link>
	<description></description>
	<lastBuildDate>Wed, 24 Sep 2025 10:12:29 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://serveravatar.com/wp-content/uploads/2025/02/cropped-favicon-32x32.png</url>
	<title>php substr | ServerAvatar</title>
	<link>https://serveravatar.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Use php substr Function Easily</title>
		<link>https://serveravatar.com/use-php-substr-function/</link>
		
		<dc:creator><![CDATA[Suresh Ramani]]></dc:creator>
		<pubDate>Tue, 22 Jul 2025 02:30:00 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[php coding]]></category>
		<category><![CDATA[php examples]]></category>
		<category><![CDATA[php for beginners]]></category>
		<category><![CDATA[php string functions]]></category>
		<category><![CDATA[php substr]]></category>
		<category><![CDATA[php tips]]></category>
		<category><![CDATA[php tutorial]]></category>
		<guid isPermaLink="false">https://serveravatar.com/?p=26412</guid>

					<description><![CDATA[<p>Ever tried cutting a slice of cake? That&#8217;s exactly what the php substr function does &#8211; but for strings! Whether you&#8217;re new to PHP or brushing up on the basics, learning how to work with string functions like substr is a must. In this guide, I&#8217;ll walk you through everything you need to know about [&#8230;]</p>
<p>The post <a href="https://serveravatar.com/use-php-substr-function/">How to Use php substr Function Easily</a> first appeared on <a href="https://serveravatar.com">ServerAvatar</a>.</p>]]></description>
										<content:encoded><![CDATA[<figure class="wp-block-image size-large"><a href="https://serveravatar.com?ref=blog" target="_blank" rel=" noreferrer noopener"><img fetchpriority="high" decoding="async" width="1024" height="127" src="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-1024x127.png" alt="Blog banner - ServerAvatar" class="wp-image-28182" srcset="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-1024x127.png 1024w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-300x37.png 300w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-768x95.png 768w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-1536x190.png 1536w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-2048x253.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>Ever tried cutting a slice of cake? That&#8217;s exactly what the <code>php substr</code> function does &#8211; but for strings! Whether you&#8217;re new to PHP or brushing up on the basics, learning how to work with string functions like <code>substr</code> is a must.</p>



<p>In this guide, I&#8217;ll walk you through everything you need to know about the <code>php substr</code> function in a friendly, easy-to-understand way. And yes, there will be plenty of examples!</p>



<h2 class="wp-block-heading" id="what-is-php-substr"><strong>What is php substr?</strong></h2>



<p>Think of&nbsp;<code><a href="https://www.php.net/manual/en/function.substr.php" target="_blank" rel="noopener nofollow" title="">php substr</a></code>&nbsp;as a string cutter. Just like you&#8217;d slice a piece of bread from a loaf,&nbsp;<code>php substr</code>&nbsp;slices part of a string from a longer one. It’s a built-in PHP function that returns a portion of a string based on the start position and length you provide.</p>



<h2 class="wp-block-heading" id="why-use-php-substr"><strong>Why Use php substr?</strong></h2>



<p>You&#8217;d use&nbsp;<code>php substr</code>&nbsp;when you want to:</p>



<ul class="wp-block-list">
<li>Extract parts of a string (like a domain name from a URL)</li>



<li>Trim down user input</li>



<li>Show only a preview of a longer text</li>



<li>Format strings for better readability</li>
</ul>



<p>If you&#8217;re working on a project that relies on specific PHP behavior, using the correct PHP version is essential. The <code>php substr</code> function behaves consistently, but subtle differences may appear in older versions. Not sure which PHP version to choose? <a class="" href="https://serveravatar.com/choose-the-right-php-version/">Here’s a detailed guide to help you pick the right PHP version for your server</a>.</p>



<h2 class="wp-block-heading" id="basic-syntax-of-php-substr"><strong>Basic Syntax of php substr</strong></h2>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>substr(string $string, int $start, int $length = ?): string</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">substr</span><span style="color: #D4D4D4">(</span><span style="color: #569CD6">string</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$string</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">int</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$start</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">int</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$length</span><span style="color: #D4D4D4"> = ?): </span><span style="color: #569CD6">string</span></span></code></pre></div>



<p>Let&#8217;s break that down:</p>



<ul class="wp-block-list">
<li><strong>$string</strong> &#8211; The input string.</li>



<li><strong>$start</strong> &#8211; Position to start slicing.</li>



<li><strong>$length</strong> <em>(optional)</em> &#8211; How many characters to return.</li>
</ul>



<h2 class="wp-block-heading"><strong>Understanding the Parameters</strong></h2>



<p>The real magic happens when you tweak the <code>start</code> and <code>length</code>:</p>



<ul class="wp-block-list">
<li>Positive <code>start</code>: Counts from the beginning (0 is the first character).</li>



<li>Negative <code>start</code>: Starts counting from the end of the string.</li>



<li>Omitted <code>length</code>: It slices from the start till the end.</li>
</ul>



<h2 class="wp-block-heading">Simple Examples to Start With</h2>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>echo substr("Hello World", 0, 5); // Output: Hello</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">substr</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&quot;Hello World&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">5</span><span style="color: #D4D4D4">); </span><span style="color: #6A9955">// Output: Hello</span></span></code></pre></div>



<p>Here, you&#8217;re saying: &#8220;Give me 5 characters starting from position 0.&#8221;</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>echo substr("Hello World", 6); // Output: World</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">substr</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&quot;Hello World&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">6</span><span style="color: #D4D4D4">); </span><span style="color: #6A9955">// Output: World</span></span></code></pre></div>



<p>In this one, since <code>length</code> is not given, it takes everything after position 6.</p>



<h2 class="wp-block-heading">Using Negative Start Values</h2>



<p>Let&#8217;s say you want the last few characters. Negative values come in handy!</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>echo substr("Hello World", -5); // Output: World</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">substr</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&quot;Hello World&quot;</span><span style="color: #D4D4D4">, -</span><span style="color: #B5CEA8">5</span><span style="color: #D4D4D4">); </span><span style="color: #6A9955">// Output: World</span></span></code></pre></div>



<p>That&#8217;s like saying: &#8220;Start 5 characters from the end.&#8221;</p>



<h2 class="wp-block-heading">What Happens If You Omit Length?</h2>



<p>If you skip the <code>length</code> parameter, PHP assumes you want everything till the end of the string from your start point.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>echo substr("PHP is fun", 4); // Output: is fun</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">substr</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&quot;PHP is fun&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">4</span><span style="color: #D4D4D4">); </span><span style="color: #6A9955">// Output: is fun</span></span></code></pre></div>



<p>Sometimes that&#8217;s exactly what you want!</p>



<h2 class="wp-block-heading">Combining substr with Other Functions</h2>



<p>You can use <code>php substr</code> with functions like <code>strpos</code> for smarter results.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>$email = "john@example.com";
$atPos = strpos($email, "@");
$username = substr($email, 0, $atPos);
echo $username; // Output: john</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #9CDCFE">$email</span><span style="color: #D4D4D4"> = </span><span style="color: #CE9178">&quot;john@example.com&quot;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #9CDCFE">$atPos</span><span style="color: #D4D4D4"> = </span><span style="color: #DCDCAA">strpos</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">$email</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&quot;@&quot;</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #9CDCFE">$username</span><span style="color: #D4D4D4"> = </span><span style="color: #DCDCAA">substr</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">$email</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">$atPos</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$username</span><span style="color: #D4D4D4">; </span><span style="color: #6A9955">// Output: john</span></span></code></pre></div>



<p>This combo is perfect for tasks like extracting usernames from emails.</p>



<h2 class="wp-block-heading">Common Mistakes and How to Avoid Them</h2>



<ul class="wp-block-list">
<li><strong>Out of range start value</strong>: If you go too far, PHP returns <code>false</code>.</li>



<li><strong>Passing non-integer start/length</strong>: Always use integers.</li>



<li><strong>Not checking for <code>false</code> return</strong>: Always validate the result before using.</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>$result = substr("Hello", 20);
if ($result === false) {
    echo "Start position out of range!";
}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #9CDCFE">$result</span><span style="color: #D4D4D4"> = </span><span style="color: #DCDCAA">substr</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&quot;Hello&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">20</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (</span><span style="color: #9CDCFE">$result</span><span style="color: #D4D4D4"> === </span><span style="color: #569CD6">false</span><span style="color: #D4D4D4">) {</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;Start position out of range!&quot;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span></code></pre></div>



<h2 class="wp-block-heading">Real-World Use Cases</h2>



<ul class="wp-block-list">
<li>Cutting excerpts from blog posts</li>



<li>Masking credit card numbers</li>



<li>Displaying the last 4 digits of a phone number</li>



<li>Shortening long titles or names</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>$cc = "1234567812345678";
echo "**** **** **** " . substr($cc, -4); // Output: **** **** **** 5678</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #9CDCFE">$cc</span><span style="color: #D4D4D4"> = </span><span style="color: #CE9178">&quot;1234567812345678&quot;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;**** **** **** &quot;</span><span style="color: #D4D4D4"> </span><span style="color: #D4D4D4">.</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">substr</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">$cc</span><span style="color: #D4D4D4">, -</span><span style="color: #B5CEA8">4</span><span style="color: #D4D4D4">); </span><span style="color: #6A9955">// Output: **** **** **** 5678</span></span></code></pre></div>



<h2 class="wp-block-heading"><strong>php substr vs. mb_substr</strong></h2>



<p>For multilingual strings or Unicode characters, always use <code>mb_substr</code>.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>echo substr("你好世界", 0, 2); // Might break
echo mb_substr("你好世界", 0, 2); // Correct</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">substr</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&quot;你好世界&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">2</span><span style="color: #D4D4D4">); </span><span style="color: #6A9955">// Might break</span></span>
<span class="line"><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">mb_substr</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&quot;你好世界&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">2</span><span style="color: #D4D4D4">); </span><span style="color: #6A9955">// Correct</span></span></code></pre></div>



<p>If you&#8217;re dealing with non-English text, <code>mb_substr</code> is the way to go.</p>



<figure class="wp-block-image size-large"><a href="https://serveravatar.com?ref=blog" target="_blank" rel=" noreferrer noopener"><img decoding="async" width="1024" height="127" src="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-1024x127.png" alt="Blog banner - ServerAvatar" class="wp-image-28184" srcset="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-1024x127.png 1024w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-300x37.png 300w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-768x95.png 768w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-1536x190.png 1536w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-2048x254.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<h2 class="wp-block-heading">Performance Tips</h2>



<ul class="wp-block-list">
<li>Use substr on shorter strings for better performance.</li>



<li>Avoid excessive chaining in loops.</li>



<li>Cache results if reused multiple times.</li>
</ul>



<p>These tiny changes can make your scripts faster and more efficient.</p>



<h2 class="wp-block-heading">When Not to Use php substr</h2>



<p>Avoid using <code>php substr</code>:</p>



<ul class="wp-block-list">
<li>When working with multibyte characters without <code>mb_*</code> support.</li>



<li>On binary data unless you&#8217;re sure how it works.</li>



<li>When you&#8217;re parsing structured data like JSON or XML (use appropriate parsers instead).</li>
</ul>



<h2 class="wp-block-heading">Useful Tips &amp; Tricks</h2>



<ul class="wp-block-list">
<li><strong>Getting first letter</strong>:</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>$letter = substr("PHP", 0, 1); // Output: P</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #9CDCFE">$letter</span><span style="color: #D4D4D4"> = </span><span style="color: #DCDCAA">substr</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&quot;PHP&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">); </span><span style="color: #6A9955">// Output: P</span></span></code></pre></div>



<ul class="wp-block-list">
<li><strong>Removing the last character</strong>:</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>$trimmed = substr("Hello!", 0, -1); // Output: Hello</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #9CDCFE">$trimmed</span><span style="color: #D4D4D4"> = </span><span style="color: #DCDCAA">substr</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&quot;Hello!&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">, -</span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">); </span><span style="color: #6A9955">// Output: Hello</span></span></code></pre></div>



<ul class="wp-block-list">
<li><strong>Loop through a string</strong>:</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">PHP</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>for ($i = 0; $i &lt; strlen($str); $i++) {
    echo substr($str, $i, 1);
}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> (</span><span style="color: #9CDCFE">$i</span><span style="color: #D4D4D4"> = </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">; </span><span style="color: #9CDCFE">$i</span><span style="color: #D4D4D4"> &lt; </span><span style="color: #DCDCAA">strlen</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">$str</span><span style="color: #D4D4D4">); </span><span style="color: #9CDCFE">$i</span><span style="color: #D4D4D4">++) {</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">substr</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">$str</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">$i</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span></code></pre></div>



<h2 class="wp-block-heading">Conclusion</h2>



<p>There you have it &#8211; your quick and easy guide to mastering <code>php substr</code>. It&#8217;s a small but powerful function that helps you chop, slice, and manipulate strings like a pro.</p>



<p>Think of <code>php substr</code> as your kitchen knife &#8211; simple, sharp, and always useful in the right context.</p>



<p>Next time you need just a part of a string, you&#8217;ll know exactly what to do!</p>



<h2 class="wp-block-heading">Frequently Asked Questions</h2>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question">What does php substr do?</h3><div class="aioseo-faq-block-answer">
<p><code>php substr</code> returns a portion of a string, starting from a specific position and optionally ending after a certain length.</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question">Can php substr return false?</h3><div class="aioseo-faq-block-answer">
<p>Yes, if the start index is out of bounds or the string is empty, <code>substr</code> returns <code>false</code>.</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question">What&#8217;s the difference between substr and mb_substr?</h3><div class="aioseo-faq-block-answer">
<p><code>mb_substr</code> supports multibyte (Unicode) characters, while <code>substr</code> does not. Use <code>mb_substr</code> for non-English strings.</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question">How can I get the last character of a string in PHP?</h3><div class="aioseo-faq-block-answer">
<p><code>mb_substr</code> suUse <code>substr($string, -1)</code> to get the last character.</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question">Can I use substr in PHP to mask data?</h3><div class="aioseo-faq-block-answer">
<p>Absolutely! You can hide parts of credit card numbers, emails, or phone numbers using <code>php substr</code>.</p>
</div></div>



<p></p><p>The post <a href="https://serveravatar.com/use-php-substr-function/">How to Use php substr Function Easily</a> first appeared on <a href="https://serveravatar.com">ServerAvatar</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
