String Manipulation In Php
There are instances where you need to manipulate the string either it was inputted or taken from the database. This tutorial will help you find out what string manipulation function you can use.
What is a String?
In PHP, and every other flavor of Web programming, a string is a variable contained between quotes with a literal value. For example, $number = “2†is a string variable, whereas $number = 2 is not. The first value is seen as text only, the latter as a numeric value. Simply put, a string is a text variable.
1. ucwords - this is used to convert a phrase or sentence into a title format.
<?php
$string ="string manipulation in php";
echo ucwords($string);
?>
2. strtoupper – this is used to capitalize every letter in a string.
<?php
$string ="string manipulation in php";
echo strtoupper($string);
?>
2. strtolower – this is used to bring all letters in a string to lowercase.
<?php
$string ="String Manipulation in PHP";
echo strtolower($string);
?>
3. str_replace – Â this function is used to replace a certain substring in a string
<?php
$string = "This tutorial is very useful for newbies";
$newstring = str_replace("useful", "difficult", $string );
echo $newstring;
?>
The str_replace() function takes 3 parameters. The first parameter is the piece of text you want to replace. The second is the text that will replace the original. The third parameter tells PHP what value to do the find and replace on.
4. strpos – find position of first occurrence of a string
Syntax: int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
<?php
$string = "Hello World";
echo "The word World is at position number: ".strpos($string,"World");
?>
5. substr – Return part of a string
Syntax: string substr ( string $string , int $start [, int $length ] )
<?php
$string = "Hello World";
$substring = substr($string,6);
echo $substring;
?>
The example above will display the word “World”.
I will post more of those functions in my next tutorial.














[...] have displayed the result, all we need to do is read the html file using cURL and using some string manipulations you can easily get what you want. In the example above you should get the value [...]
[...] have displayed the result, all we need to do is read the html file using cURL and using some string manipulations you can easily get what you want. In the example above you should get the value [...]
[...] have displayed the result, all we need to do is read the html file using cURL and using some string manipulations you can easily get what you want. In the example above you should get the value [...]
[...] have displayed the result, all we need to do is read the html file using cURL and using some string manipulations you can easily get what you want. In the example above you should get the value 47.499 Related [...]
[...] Google have displayed the result, all we need to do is read the html file using cURL and using somestring manipulations you can easily get what you want. In the example above you should get the value 47.499 Tags: btng, [...]
[...] have displayed the result, all we need to do is read the html file using cURL and using some string manipulations you can easily get what you want. In the example above you should get the value [...]