> Online tutorial : String in php
Showing posts with label String in php. Show all posts
Showing posts with label String in php. Show all posts

trim() in php

trim() function remove the whitespaces and other predefined character from tht both side of a string

Syntax
trim(character)
Example

<?php
$str=" hello india";
echo "without trim".$str;
// remove whitespaces from the both the string
echo "with trim".trim($str);

?>

Output
without trom: hello india
with trim:hello india

substr

substr() function returns the part of string
Syntax
substr(string,start,length)
Example

<?php
// returns the part of string
echo substr("hello india",6);
echo substr("hello india",6,5);
?>

Output
india
india

strpos() in php

strpos() function returns the position of the first occurrence of string inside another string.
Syntax
strpos(string,find,start)
Example

<?php
// returns the position of the first occurrence of string
echo strpos("hello india","io");
?>




Output
6

strlen() in php

The strlen() function returns the length of string
Syntax
strlen(string)
example

<?php
// returns the length the string
echo strlen("hello india");
?>

Output
11

strrev() in php

The strrev() function reverses the string
Syntax
strrev(string)
Example


<?php
echo strrev("one line");
?>

Output
enil eno

str_repeat() in php

The str_repeat() function repeats a string specified number of times.
Syntax
str_repeat(string,repeat)
Example

<?php
echo str_repeat("india",4);
?>

output
indiaindiaindiaindia

nl2br() in php

The nl2br() function inserts HTML line breaks(
)in front of each newline (\n)in a string
Syntax
nl2br(string)
Example

<?php
echo nl2br("one line .\n another line");
?>

Output

one line
another line

The html code look like this 

one line<br/>
another line

join() in php

join() function in php
The join()returns the string from the elements of an array.The join() function is an alias of the implode function
Syntax
join(separator,array)
Example


<?php
$str='hello india';
// it is breaks the string into an array
$exp_str=explode("",$str);
print_r($exp_str);
// it returns the string from the elements of an array
echo join("",$exp_str);
?>
Output:

Array([0]=>hello
[1]=>india)

hello india

implode() in php

The implode()function returns a string from the element of an array

Note
  • The implode() function accept its parameter in either order.How ever for consistency with explode().you should use the document order of argument .The separator parameter of imploded is optional.
Syntax:

implode(separator,array)

Example


<?php
$str='hello india';
// it is breaks the string into an array
$exp_str=explode("",$str);
print_r($exp_str);
// it returns the string from the elements of an array
echo implode("",$exp_str);
?>

Output:
Array([0]=>hello
[1]=>india)
hello india

Explode() in php

Explode()
The explode() functon breaks a string into an array
Note
Separator cannot be empty
Synatx
explode(separator,string,limit)
Example

<?php
$str="welcome to india";
echo explode("",$str);
?>
Output
Array([0]=>hello
[1]=>to
[2]=>india)

chunk_split() in php

chunk_split() in php
the chunk_split() function splits a string into a series of smaller parts
Note
This function does not alter the original string
syntax
chunk_split(string,length,end)
Example

<?php
$str="hello india";
// we will split the string after each character and add a"." after each split
echo chunk_split($str,1,'.');
?>

Output
h.e.l.l.o. .i.n.d.i.a.

chr() in php

chr()
The chr() function returns a character from the specified ASCII value
syntax
chr(ascii)
Example

<?php
// returns a character from the specified ASCII value
echo chr(52);
echo chr(052);
?>
Output
4
*

chop in php

chop()
The chop() function will remove a white space or other predefined character from the right end of string.This function is an alias the rtrim()function
Syntax
chop(string,charlist)
Example

<?php
$str="welcome to   ";
echo $str;
echo "india";
echo "<br>";
//remove the whitespace from the right end of a string
ech chop($str);
echo "india";
?>

Output
welcome to india
welcome toindia

addcslashes() and backcslashes() in php

addcslashes() and backcslashes()
The addcslashes() function return a string with backslashes in front of the specified character
Syntax
string addcslashes( string $str,string $charlsit)

Example

<?php
$str="welcome to india";
echo $str;
echo addcslashes($str,'c');
?>


Output:



wel\come to india

backcslashes()
The backcslashes() function removed the character added from addcslashes() function
Syntax:

backcslashes(string)

<?php
echo backcslashes("wel\come to india");
?>
Output
welcome to india

Strpos() function in php

strpos() function
The strpos() function is used to search for character within a string.
If a match is found, this function will return the position of the first match.
If no match is found, it will return FALSE.
Let's see if we can find the string "world" in our string:

< ?php
echo strpos("Hello world!","world");
? >

The output of the code above will be:

6

The position of the string "world" in our string is position 6. The reason that it is 6 (and not 7), is that

strlen() function in php

strlen() function
The strlen() function is used to return the length of a string.
Let's find the length of a string:

<?php
echo strlen("Hello world!");
? >

The output of the code above will be:
12
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).

Concatenation operator in php

The Concatenation Operator
There is only one string operator in PHP.
The concatenation operator (.) is used to put two string values together.
To concatenate two string variables together, use the concatenation operator:

Example

?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?

The output of the code above will be:
Hello World! What a nice day! 

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 (a space character), to separate the two strings.

String in php

PHP String Variables
A string variable is used to store and manipulate text.
String Variables in PHP
String variables are used for values that contains characters.
In this chapter we are going to look at the most common functions and operators used to manipulate
strings in PHP.
After we create a string we can manipulate it. A string can be used directly in a function or it can be stored in a variable.
Below, the PHP script assigns the text "Hello World" to a string variable called $txt:




?php
$txt="Hello World";
echo $txt;
?

The output of the code above will be:
Hello World
Now, lets try to use some different functions and operators to manipulate the string.

Function in String
strops()
strlen
Chunksplit()
addcslashes() and backcslahes()
chop()
chr()
implode()
nl2br()
strrev()
strlen()
substr()
trim()