Spiga

PHP Tutorial : String Operations

Operating string so important in programming especially in PHP. Why so important? Because the work with php, is building a good information system, facilities to make searching the web, and various other things related to web development is not separated from the operating string. So many functions that have a string operation, and also a lot of string functions that are frequently used, so that authors need to share a few articles about this string operation. For the first part of the article the author will explain the function of which has 2 additional tasks other than the search string. Among other :

  • substr()
  • strstr()
Substr(), has a function to retrieve one or more characters from a variable. Implementation, such as taking the characters generated from the function DATE() is mysql. Sample usage is as follows :
<?php

$tanggal = '012409';
$bulan = substr($date, 0, 2);

$hari = substr($date, 2, 2);
$tahun = substr($date, -2);

echo "$hari/$bulan/$tahun";

?>
The result is:
24/01/09

Explanation is as follows:

$month = substr($date, 0, 2);
Take a 2-digit characters from the front, the result is 01.

$day = substr($date, 2, 2);
Take the characters from 2 to 3 digits. Because the string has a sequence beginning 0, then the number 2 in the variable $date have in order to position 2. The result is 24.

To prove it:
<?php
$date ='012409 ';
echo $date [2];
?>
the results are :
2

And the last is
$year = substr($date, -2); This means take 2 characters from the back. The result is 09.

Strstr(), this function is used to return all the strings behind the string searched. For example there is a sentence:
"Smartness radiated from the eternal pure handsome. - Al-k",
the characters are searching "--",
the results of the function strstr() is "- Al-k"

Example of use:

The result is:
- Al-k

Implementation of strstr() example like this:

Explanation is as follows:

if ($theword = strstr($sentence, $alert)) ( if the characters in the search in this case the $sign in the string $sentence, then return the string that are behind these variables into the $theword.

echo 'The phrase is in a cool earlier by'."'". substr($theword, strlen($sign ))."'";

part is essentially substr($theword, strlen($sign))

If the change in value is actually
substr( "- Al-k", 2);

show the search string with a pass mark of --.

The result is:
Al-k


So that the results of the integrity of the script

Is:
Expression earlier in a dogged by 'Al-k'


Strstr(), this function is used to return all the strings behind the string searched. For example there is a sentence: "Smartness radiated from the eternal pure handsome. - Al-k",
the characters are searching "--",
the results of the function strstr() is "- Al-k"

Example of use:
<?php

$sentence = "smartness radiated from the eternal pure handsome. - Al-k";
$sign ='--';
$theword = strstr($sentence, $sign);
echo $theword;

?>

The result is:
- Al-k


Implementation of strstr() example like this:

<?php

$sentence = "smartness radiated from the eternal pure handsome. - Al-k";
$sign ='--';

if($theword = strstr($sentence, $alert)) (
echo 'The phrase is in a cool earlier by'."'". substr($theword, strlen($sign))."'";
Else()
echo "dont have any expression!";
)

?>

Explanation is as follows:

if ($theword = strstr($sentence, $alert)) (

if the characters in the search in this case the $sign in the string $sentence, then return the string that are behind these variables into the $theword.

echo 'The phrase is in a cool earlier by'."'". substr($theword, strlen($sign ))."'";

part is essentially substr($theword, strlen($sign))

If the change in value is actually

substr( "- Al-k", 2)

show the search string with a pass mark of --.

The result is:
Al-k

So that the results of the integrity of the script
<? php

$sentence = "smartness radiated from the eternal pure handsome. - Al-k";
$sign ='--';

if ($theword = strstr($sentence, $alert)) (
echo 'The phrase is in a cool earlier by'."'". substr($theword, strlen($sign ))."'";
Else()
echo "Dont have any expression!";
)

?>

The result is:
Expression earlier in a dogged by 'Al-k'


OK.. thats it for now, see you at next tutorial!

This tutorial translated from: http://www.ilmuwebsite.com/

1 comments:

  Hijabi Girl

13/10/21 2:06 AM

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. String functions php

Post a Comment