"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Tuesday, June 12, 2018

Strings in PHP


What is String in PHP ?

They are sequences of characters, like "Hello World".
A 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';
Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.
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.
Strings that are delimited by double quotes (as in "this") are preprocessed in both the following two ways by PHP −
·        Certain character sequences beginning with backslash (\) are replaced with special characters
·        Variable names (starting with $) are replaced with string representations of their values.

The escape-sequence replacements are:

  1. ·         \n is replaced by the newline character
  2. ·         \r is replaced by the carriage-return character
  3. ·         \t is replaced by the tab character
  4. ·         \$ is replaced by the dollar sign itself ($)
  5. ·         \" is replaced by a single double-quote (")
  6. ·         \\ is replaced by a single backslash (\)
Example:
<?php
$my_str = 'World';
echo "Hello, $my_str!<br>";
echo 'Hello, $my_str!<br>';
echo '<pre>Hello\tWorld!</pre>';
echo "<pre>Hello\tWorld!</pre>";
echo 'I\'ll be back';
?>
Output:
Hello, World!
Hello, $my_str!

Hello\tWorld!
Hello       World!
I'll be back

Manipulating PHP Strings

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. Here are the examples of some of these functions.

1)String Concatenation

we concatenate two string variables together, using the dot (.) operator.
Example:
<?php
$string1=”Hello World”:
$string2=”1234”:
echo $string1.” ”.$string2;
?>
If we look at the code above you see that we used the concatenation operator two times. This is because we had to insert a third string.
Between the two string variables we added a string with a single character, an empty space, to separate the two variables.

2.Calculating the Length of a String

The strlen() function is used to calculate the number of characters inside a string or to find the length of a string.
It also includes the blank spaces inside the string.
Example:
<?php
$my_str = 'Welcome to PHP';
// Calculating and displaying string length
echo strlen($my_str);
?>
The length of a string is often used in loops or other functions, when it is important to know when the string ends. (i.e. in a loop, we would want to stop the loop after the last character in the string)

3.Using the strpos() function

The strpos() function is used to search for a string or character within a string.
If a match is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE.
<?php
echo strpos(“Hello world!”,”world”);
?>
This will produce the following result −6
As you see the position of the string "world" in our string is position 6. The reason that it is 6, and not 7, is that the first position in the string is 0, and not 1.

4.Counting Number of Words in a String

The str_word_count() function counts the number of words in a string.
Example:
<?php
$my_str = 'The quick brown fox jumps over the lazy dog.';
 // Calculating and displaying number of words
echo str_word_count($my_str);
?>
Output:
9

5.Replacing Text within Strings

The str_replace() replaces all occurrences of the search text within the target string.
<?php
$my_str = 'If the facts do not fit the theory, change the facts.';
 // Display replaced string
echo str_replace("facts", "truth", $my_str);
?>
Output:
If the truth do not fit the theory, change the truth.
You can optionally pass the fourth argument to the str_replace() function to know how many times the string replacements was performed, like this.
Example:
<?php
$my_str = 'If the facts do not fit the theory, change the facts.';
// Perform string replacement
str_replace("facts", "truth", $my_str, $count);
 // Display number of replacements performed
echo "The text was replaced $count times.";
?>                                          
Output:
The text was replaced 2 times.

6.Reversing a String

The strrev() function reverses a string.
Example:
<?php
$my_str = 'You can do anything, but not everything.';
// Display reversed string
echo strrev($my_str);
?>                                          
Output:
.gnihtyreve ton tub ,gnihtyna od nac uoY
7.Using HTML Tags inside print:
we can used HTML tag inside the string variable –
Example:
<?php
$name=”Sachin <br/> “.”Tendulkar”;
print($name);
?>
Output :
Sachin
Tendulkar

No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template