PHP String Functions

Last updated on Aug 18, 2020
  • 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.

Q1. How to repeat a string to a specific number of times in php?
Answer

In PHP, the str_repeat() function, which is inbuilt is used to repeat a string for a specific number of times. This str_repeat() function creates new strings by repeating the given string ‘n’ number of times as specified.

Here is the syntax of the str_repeat() function: str_repeat(string, repeat)

Example

Here is an example demonstrating the same:
echo str_repeat("BIQ ", 2);

Output

BIQ BIQ

Q2. List the most commonly use string functions in PHP?
Answer

Here is a list of the most commonly used PHP String Functions while programming:

  • substr() function: This function is used in PHP to give you access to a substring having a specific start and endpoints within the string.
    Syntax:
    string substr(string string, int start[, int length] );
  • strlen() function: This function is used in PHP to check the character length of a string.
    Syntax:
    echo strlen(“string”)
  • str_replace() function: This function is used in PHP as a find and replace function within any string.
    Syntax: str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) : mixed
  • trim() function: This function is used in PHP to trim all the whitespace from the starting point to the endpoint in any
    string.
    Syntax: trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] ) : string
  • strpos() function: This function is used in PHP to find the first occurrence of a substring within a string.
    Syntax: strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
    In the above syntax, it actually commands PHP to find the first occurrence of the substring needle in the string haystack.
  • strtolower() function: This function is used in PHP to convert all the characters in a string to lowercase.
    Syntax: strtolower ( string $string ) : string
  • strtoupper() function: Well, obviously this function is used to convert all the characters in a string to the upper case.
    Syntax: strtoupper ( string $string ) : string

In the above command, it returns with part of the haystack string starting from the point of the needle to the end of the haystack.

Q3. How to get last character of string in php?
Answer

To find the last character of a PHP string, use the substr() function in the following way:

Example

substr("best interview question", -1);

Output:
n

Q4. How to remove HTML tags from a string using PHP?
Answer

Here is an example to show how to strip the HTML tags from a PHP string.

Example

$text = '<p>Test paragraph.</p><a href="#fragment">Other text</a>';
echo strip_tags($text, '<p><a>');

Output

Test paragraph.

Q5. How to get string between two characters in php?
Answer

Suppose we have a

$string = “[Hello there I’m=testing]”

And we want to extract the string placed between the characters between = and ].

Here’s how to do it using PHP string functions:

Example

$str = "[Hello there I’m=testing]";
$from = "=";
$to = "]";

echo getStringBetween($str,$from,$to);

function getStringBetween($str,$from,$to)
{
    $sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
    return substr($sub,0,strpos($sub,$to));
}

Output:
Testing

Q6. What is the difference between print and echo in php?
Answer

The echo and print are basically the same functions, display output data on the screen. However, there are some minor differences between the two such as:

Echo Print
It has no return value. It has a return value of 1, hence can be used in expressions.
It can work with multiple parameters. It can function with only one argument at a time.
It is a bit faster than Print A bit slower than Print.

Here’s an example of each Echo and Print to clearly demonstrate their proper usage:

Example
Echo

echo "PHP is Fun!";

Print

print "PHP coding is Fun!";

Q7. What is String in PHP?
Answer

In PHP, String is a collection of characters. It is one of the data types. We can declare variable and assign string characters to it and we can directly use them with echo statement.

Example

$var1 = “This is good interview Question”
echo $var1;

Q8. How to calculate the length of a string?
Answer

We can calculate the length of a string with strlen() function in PHP.

$var1 = “This is good interview Question”;
echo strlen($var1);

Q9. What is the use of strpos in PHP?
Answer

strops() method is used to get the position of the first occurrence of a string inside another string.

Example

$mainString = 'Best Interview Question';
echo strpos($mainString, "Interview");
// OUTPUT : 5

Q10. What is a substring in PHP?
Answer

substr() is a substring of PHP. It is an inbuilt function which is used to extract a part of the string.

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...