PHP String Functions

Last updated on Mar 24, 2024
  • Share
PHP Strings

A string is basically a collection of characters. It is a data type in PHP. A function in PHP is a unit of code that returns a value based on an input or parameter and processing based on the codes.

Functions are basically a set of in a string that is used to manipulate this data type for different uses and requirements. Common PHP String functions include strrev(), strpos(), str_replace() and many more. The basic need of a string function is to help in completing specific tasks relating to a string. It could be reversing, replacing or even searching for text within strings.

PHP String functions are used to manipulate strings in order to perform a specific task/function when the user has given input. This is one of the most commonly used functions in PHP and if you are aspiring for a job as an SDE in PHP, learning string functions is a must.

Below is a list of the most commonly asked interview questions regarding String functions in PHP. Have a look or even download it in PDF format to read it later.

Most Frequently Asked PHP String Functions

Here in this article, we will be listing frequently asked PHP Strings and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

Q11. What is TRIM function in PHP?
Answer

trim() removes all blank spaces or whitespace or other (specified) characters from both sides of a string.

$var = “ Best Interview Questions ”
echo $var;

Types:
  • ltrim() – It removes whitespace/other specified characters from the left side of a string.
  • rtrim() - It removes whitespace/other specified characters from the right side of a string.
Example

$var = “Best Interview Questions”;
ltrim(“Best”, $var);
rtrim(“Questions”, $var);

Q12. How to replace a text in a string with another text in php?
Answer

We can use str_replace() method to replace a text in a string with another string.

Example

str_replace("interview","interviews","Best interview questions!");

Q13. How to convert a string to uppercase in php?
Answer
We can use strtoupper() method to convert any string to Uppercase.
Example

strtoupper("Best Interview Questiomns");
// Output: BEST INTERVIEW QUESTIONS

Q14. How to insert a line break in php string?
Answer

You can use nl2br() to insert line break in the string.

Example

nl2br("Best Interview \n Questions");

Q15. How to get the position of the character in a string in php?
Answer

To find the position of a character inside a string in PHP, use the strpos() function in the following way:
$pos = strpos($string, $char);

Q16. How to convert a string to lowercase in php?
Answer

You can use strtolower() to convert any string to lowercase.

Example

strtolower('Best Interview Questions');
// OUTPUT: best interview questions

Q17. How to find character in string in php?
Answer
Q18. How to limit string length in php?
Answer

To limit the characters in a PHP string, use the following code:

if (strlen($str) > 10)
    $str = substr($str, 0, 6) . '...';

In the above code, if the str value is greater than 10, the output will display the final result from 0 to the 6th character of the said string.

Q19. How to read each character of a string in php?
Answer

For this, the str_split() function can be used. It will convert your string into an array of individual characters where you can enter the start and endpoint of the characters you wish to see.

Example

$str = "Hello";
$arr1 = str_split($str);
print_r($arr1);

Output:

Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
)

Q20. How to replace string between two characters in php?
Answer

Here’s a code to replace all the text between two specific characters:

Example

function replace_all_text_between($str, $start, $end, $replacement) {

    $replacement = $start . $replacement . $end;

    $start = preg_quote($start, '/');
    $end = preg_quote($end, '/');
    $regex = "/({$start})(.*?)({$end})/";

    return preg_replace($regex,$replacement,$str);
}

Reviewed and verified by Umesh Singh
Umesh Singh

My name is Umesh Singh having 11+ experience in Node.JS, React JS, Angular JS, Next JS, PHP, Laravel, WordPress, MySQL, Oracle, JavaScript, HTML and CSS etc. I have worked on around 30+ projects. I lo...