Current Location: Home> Latest Articles> How to Use the substr Function to Handle Overly Long PHP Strings?

How to Use the substr Function to Handle Overly Long PHP Strings?

gitbox 2025-10-01
<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// This is a PHP code snippet unrelated to the main article content</span></span><span>
</span><span><span class="hljs-variable">$placeholder</span></span><span> = </span><span><span class="hljs-string">"This code snippet is unrelated and used only for demonstration."</span></span><span>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$placeholder</span></span><span>;
</span><span><span class="hljs-meta">?&gt;</span></span><span>
<p><hr></p>
<p></span><?php<br>
/**</p>
<ul>
<li>
<p>Article Body: How to Use the substr Function to Handle Overly Long PHP Strings?</p>
</li>
<li></li>
<li>
<p>In PHP development, you often encounter overly long strings, such as when retrieving article content, user comments, or other text data from a database.</p>
</li>
<li>
<p>Directly outputting these strings may disrupt page layout or slow down loading. To handle this, you can use PHP’s built-in substr function to truncate the string.</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>Basic Usage of substr</p>
</li>
</ol>
</li>
<li>
<p>The basic syntax of substr is as follows:</p>
</li>
<li></li>
<li>
<p>substr(string $string, int $start, ?int $length = null): string</p>
</li>
<li></li>
<li>
<ul>
<li>
<p>$string: The original string to be processed.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$start: The starting position; 0 means from the beginning of the string.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$length: Optional parameter specifying the length to extract. If omitted, it extracts to the end of the string.</p>
</li>
</ul>
</li>
<li></li>
<li>
<p>Example:<br>
*/<br>
$original = "PHP is a popular server-side scripting language, ideal for developing websites and applications.";<br>
$shortened = substr($original, 0, 20);<br>
echo $shortened . "..."; // Output: PHP is a popular server-side...<br>
?></p>
</li>
</ul>
<p><?php<br>
/**</p>
<ul>
<li>
<p>2. Handling Multibyte Characters</p>
</li>
<li>
<p>When dealing with Chinese, Japanese, or other multibyte characters, using substr directly may cause garbled output.</p>
</li>
<li>
<p>The solution is to use the mb_substr function:<br>
*/<br>
$original_cn = "This is a Chinese string example for testing truncation functionality.";<br>
$shortened_cn = mb_substr($original_cn, 0, 10, "UTF-8");<br>
echo $shortened_cn . "..."; // Output: This is a Chinese string...<br>
?></p>
</li>
</ul>
<p><?php<br>
/**</p>
<ul>
<li>
<p>3. Dynamic Truncation with Ellipsis</p>
</li>
<li>
<p>You can dynamically check string length and only truncate with ellipsis if it exceeds a certain limit:<br>
*/<br>
function truncateString($string, $length = 50) {<br>
if (mb_strlen($string, "UTF-8") > $length) {<br>
return mb_substr($string, 0, $length, "UTF-8") . "...";<br>
} else {<br>
return $string;<br>
}<br>
}</p>
</li>
</ul>
<p>$example_text = "Using substr or mb_substr functions, we can effectively control the length of displayed text, improving user experience.";<br>
echo truncateString($example_text, 30); // Output: Using substr or mb_substr functions...<br>
?></p>
<p><?php<br>
/**</p>
<ul>
<li>
<p>Summary:</p>
</li>
<li>
<p>Using substr or mb_substr makes it easy to control PHP string length:</p>
</li>
<li>
<ul>
<li>
<p>For English or single-byte characters, substr is sufficient.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>For Chinese or multibyte characters, mb_substr is recommended.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>Combining length checks with ellipsis ensures cleaner and user-friendly page displays.<br>
*/<br>
?></p>
</li>
</ul>
</li>
</ul>
<p data-is-last-node="" data-is-only-node=""><?php<br>
// Footer unrelated code example<br>
$footer_note = "End of example.";<br>
echo $footer_note;<br>
?><br>
</span>