By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
What do PHP String functions do? 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.
Q 1. How to repeat a string to a specific number of times in php? 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
Q 2. List the most commonly use string functions in PHP. Here is a list of the most commonly used PHP String Functions:
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.
Q 3. How to get last character of string in php? To find the last character of a PHP string, use the substr() function in the following way:
Example: substr("All The Useful", -1);
Output:
l
Q 4. How to remove HTML tags from a string using PHP? 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>');
Test paragraph.
Q 5. How to get string between two characters in php?
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)); }
Testing
Q 6. What is the difference between print and echo in php? 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 Vs 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!";
Q 7. What is String in PHP? 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 = 'All The Useful' echo $var1;
Q 8. How to calculate the length of a string? We can calculate the length of a string with strlen() function in PHP.
$var1 = 'All The Useful'; echo strlen($var1);
Q 9. What is the use of strpos in PHP? strops() method is used to get the position of the first occurrence of a string inside another string.
Example: $mainString = 'All The Useful';
echo strpos($mainString, "The");
// OUTPUT: 4
Q 10. What is a substring in PHP? substr() is a substring of PHP. It is an inbuilt function which is used to extract a part of the string.
Q 11. What is TRIM function in PHP? trim() removes all blank spaces or whitespace or other (specified) characters from both sides of a string.
$var = ' All The Useful ' 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 = 'All The Useful';
ltrim('All', $var); rtrim('Useful', $var);
Q 12. How to replace a text in a string with another text in php? We can use str_replace() method to replace a text in a string with another string.
Example: str_replace("useful","very useful","All The Useful!");
Q 13. How to convert a string to uppercase in php? We can use strtoupper() method to convert any string to Uppercase.
Example: strtoupper("All The Useful"); // Output: ALL THE USEFUL
Q 14. How to insert a line break in php string? You can use nl2br() to insert line break in the string.
Example: nl2br("All The \ Useful");
Q 15. How to get the position of the character in a string in php? To find the position of a character inside a string in PHP, use the strpos() function in the following way: $pos = strpos($string, $char);
Q 16. How to convert a string to lowercase in php? You can use strtolower() to convert any string to lowercase.
Example: strtolower('All The Useful'); // OUTPUT: all the useful
Q 17. How to find character in string in php? You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() the function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .
Q 18. How to limit string length in php? 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.
Q 19. How to read each character of a string in php? 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);
Array ( [0] => H [1] => e [2] => l [3] => l [4] => o )
Q 20. How to replace string between two characters in php? 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); }
Q 21. How to replace multiple characters in a string in php?
To replace multiple characters from the string in php:
Specify the string Specify the string using this code: $my_str = ‘\”\1+2/32:2-3/43′;
Replace multiple characters from the string Replace multiple characters from the string using this code: echo str_replace(str_split(‘\/:*?”<>|+-‘), ‘ ‘, $my_str).”\n”;
Q 22. How to convert array to string in php? With the help of implode() function in PHP, we can convert arrays into strings. It returns the string.
implode(separator,array);
Example: $arr = array("All", "The", "Useful"); echo implode(" ",$arr);
OUTPUT
All The Useful
Q 23. How to find string length in php without strlen? <?php
$string = "ved";
$string_lenght = 0; while($string[$string_lenght] != null){ $string_lenght++; }
echo "Total String Length : ". $string_lenght;
?>
Q 24. How to check empty string in php? To check empty string in PHP, we can use the empty() function.
Example: if(empty($var1)){ // True } else { // False }
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.