String handling is one of the most important parts of PHP development. Whether you are working with user input, formatting output, or building dynamic content, understanding string functions is essential.
If you have already explored basic string escaping functions like addcslashes() and addslashes(), now it's time to move forward and understand more advanced string operations.
In this guide, you will learn PHP string functions like chr(), chunk_split(), and convert_cyr_string() with real examples.
Learn PHP string functions chr(), chunk_split(), and convert_cyr_string() with examples
PHP chr() Function
The chr() function is used to convert an ASCII value into its corresponding character. This allows developers to dynamically generate characters instead of hardcoding them.
This function becomes very powerful when used in loops or dynamic systems. For example, if you are generating alphabets programmatically or working with numeric character codes, chr() makes your work much easier.
If you are already familiar with string transformation functions like bin2hex(), then chr() helps you perform the reverse type of operation by converting numbers into readable characters.
Syntax
chr(ascii_value)
Basic Example
<?php
echo chr(65); // Output: A
?>
Example with if-else
<?php
$num = 70;
if($num >= 65 && $num <= 90){
echo chr($num);
}else{
echo "Invalid ASCII value";
}
?>
Example with foreach
<?php
$nums = [65, 66, 67];
foreach($nums as $num){
echo chr($num) . "<br>";
}
?>
Real-Life Use Case
Used in generating alphabets, encoding systems, dynamic password creation, and working with APIs.
PHP chunk_split() Function
The chunk_split() function is used to split a string into smaller parts. This is especially useful when dealing with long strings that need to be formatted for readability.
Before formatting long strings, it is often a good practice to clean them. You can use chop() function to remove unwanted spaces and then apply chunk_split() for better formatting.
This function is widely used in formatting serial numbers, license keys, and encoded data.
Syntax
chunk_split(string, length, end)
Basic Example
<?php
echo chunk_split("HELLOWORLD", 2, "-");
?>
Example with if-else
<?php
$text = "HELLOWORLD";
if(strlen($text) > 5){
echo chunk_split($text, 2, "-");
}else{
echo "Text too short";
}
?>
Example with foreach
<?php
$texts = ["HELLOWORLD", "PHPCODE"];
foreach($texts as $txt){
echo chunk_split($txt, 2, "-") . "<br>";
}
?>
Real-Life Use Case
Used in formatting credit cards, serial keys, API tokens, and improving readability.
PHP convert_cyr_string() Function
The convert_cyr_string() function converts strings between Cyrillic encodings. It is mainly used in multilingual applications or legacy systems.
If you are working with encoding-related transformations, you may also explore bin2hex() function to understand how data encoding works in PHP.
Although this function is not commonly used in modern applications, it is still important for understanding encoding concepts.
Syntax
convert_cyr_string(string, from, to)
Basic Example
<?php
echo convert_cyr_string("example", "a", "d");
?>
Example with if-else
<?php
$text = "example";
if(!empty($text)){
echo convert_cyr_string($text, "a", "d");
}else{
echo "Empty string";
}
?>
Example with foreach
<?php
$texts = ["text1", "text2"];
foreach($texts as $txt){
echo convert_cyr_string($txt, "a", "d") . "<br>";
}
?>
Real-Life Use Case
Used in legacy applications and systems dealing with Cyrillic language encoding.
Conclusion
In this article, we explored chr(), chunk_split(), and convert_cyr_string() in detail. These functions help in character generation, string formatting, and encoding conversion.
To build strong fundamentals, make sure you also check previously covered functions like addcslashes(), addslashes(), bin2hex(), and chop().
Frequently Asked Questions (FAQ)
What is chr() function in PHP?
The chr() function converts ASCII values into characters and is widely used in dynamic string generation.
What is chunk_split() used for?
It splits long strings into smaller parts for formatting and readability.
Is convert_cyr_string() still useful?
It is mainly used in legacy systems that handle Cyrillic encoding.
Can I use these functions together?
Yes, depending on your use case, you can combine them for advanced string manipulation.