In this tutorial you will learn how to store and manipulate strings in PHP. Show
What is String in PHPA string is a sequence of letters, numbers, special characters and arithmetic values or combination of all. The simplest way to create a string is to enclose the string literal (i.e. string characters) in single quotation marks ('), like this: $my_string = 'Hello World'; You can also use double quotation marks ("). However, single and double quotation marks work in different ways. Strings enclosed in single-quotes are treated almost literally, whereas the strings delimited by the double quotes replaces variables with the string representations of their values as well as specially interpreting certain escape sequences. The escape-sequence replacements are:
Here's an example to clarify the differences between single and double quoted strings:
Manipulating PHP StringsPHP provides many built-in functions for manipulating strings like calculating the length of a string, find substrings or characters, replacing part of a string with different characters, take a string apart, and many others. Here are the examples of some of these functions. Calculating the Length of a StringThe
Counting Number of Words in a StringThe
Replacing Text within StringsThe
The output of the above code will be: If the truth do not fit the theory, change the truth. You can optionally pass the fourth argument to the
The output of the above code will be: The text was replaced 2 times. Reversing a StringThe
The output of the above code will be: .gnihtyreve ton tub ,gnihtyna od nac uoY PHP String ReferenceFor a complete list of useful string functions, please check out PHP String Reference. A string is a sequence of characters, like "Hello world!". PHP String FunctionsIn this chapter we will look at some commonly used functions to manipulate strings. strlen() - Return the Length of a StringThe PHP ExampleReturn the length of the string "Hello world!": <?php Try it Yourself » str_word_count() - Count Words in a StringThe PHP ExampleCount the number of word in the string "Hello world!": <?php Try it Yourself » strrev() - Reverse a StringThe PHP ExampleReverse the string "Hello world!": <?php Try it Yourself » strpos() - Search For a Text Within a StringThe PHP ExampleSearch for the text "world" in the string "Hello world!": <?php Try it Yourself » Tip: The first character position in a string is 0 (not 1). str_replace() - Replace Text Within a StringThe PHP ExampleReplace the text "world" with "Dolly": <?php Try it Yourself » Complete PHP String ReferenceFor a complete reference of all string functions, go to our complete PHP String Reference. The PHP string reference contains description and example of use, for each function! PHP ExercisesWhat are the different string manipulation functions PHP?PHP String Functions. Which are the various types of strings in PHP?A string literal can be specified in four different ways:. single quoted.. double quoted.. heredoc syntax.. nowdoc syntax.. What are the various methods to manipulate strings?String Manipulation Techniques. Extract or truncate the first few characters of a string,. Extract or truncate some characters at the end of the string,. Find out the length (number of characters) of a string,. Convert a string from lowercase to UPPERCASE or vice-versa,. Check if a character has been used in a string,. What is string manipulation PHP?PHP provides many built-in functions for manipulating strings like calculating the length of a string, find substrings or characters, replacing part of a string with different characters, take a string apart, and many others.
|