Hope below functions will help you in your current and coming projects for PHP language.
Here we will talk about string and array functions and their usage.
String functions:
substr_count(): Count sub-string from given string.
Example:
<?php
echo substr_count("someone.good@gmail.com", ".");
?>
Result 2 because Dot (.) will be count in the above example.
str_replace(): Replace characters or words from the given string.
Example:
<?php
echo str_replace('Hello', 'Beautiful', 'Hello World!');
?>
Result:
Beautiful World!
preg_match(): It is used for matching pattern.
Example:
<?php
if(preg_match("/\bscripting\b/i", "PHP is the server side scripting language"))
echo 'A match was found';
else
echo 'A match was not found';
?>
Result:
A match was found
Array functions:
array_slice(): Extract values from given array.
Example:
<?php
$data = array('a', 'b', 'c', 'd', 'e');
$new_data = array_slice($data, 1);
foreach($new_data as $value){
echo $value . "\n";
}
?>
Result:
b
c
d
e
in_array(): Check value exists or not.
Example
<?php
$data = array('football', 'bike', 'car');
$value = 'bike';
if(in_array($value, $data)) echo 'Yes I am present';
else 'Sorry I am not here';
?>
Result:
Yes I am present
array_flip(): Flip all the keys & values.
Example:
<?php
$arr = array('a'=>'A', 'b'=>'B', 'c'=>'C');
$flip_this = array_flip($arr);
foreach($flip_this as $key=>$value){
echo 'Key:' . $key . ' and Value:' . $value . "\n";
}
?>
Result:
Key:A and Value:a
Key:B and Value:b
Key:C and Value:c
If you friends wanted to discuss here anything related to PHP functions and tips, please share those things.
Here we will talk about string and array functions and their usage.
String functions:
substr_count(): Count sub-string from given string.
Example:
<?php
echo substr_count("someone.good@gmail.com", ".");
?>
Result 2 because Dot (.) will be count in the above example.
str_replace(): Replace characters or words from the given string.
Example:
<?php
echo str_replace('Hello', 'Beautiful', 'Hello World!');
?>
Result:
Beautiful World!
preg_match(): It is used for matching pattern.
Example:
<?php
if(preg_match("/\bscripting\b/i", "PHP is the server side scripting language"))
echo 'A match was found';
else
echo 'A match was not found';
?>
Result:
A match was found
Array functions:
array_slice(): Extract values from given array.
Example:
<?php
$data = array('a', 'b', 'c', 'd', 'e');
$new_data = array_slice($data, 1);
foreach($new_data as $value){
echo $value . "\n";
}
?>
Result:
b
c
d
e
in_array(): Check value exists or not.
Example
<?php
$data = array('football', 'bike', 'car');
$value = 'bike';
if(in_array($value, $data)) echo 'Yes I am present';
else 'Sorry I am not here';
?>
Result:
Yes I am present
array_flip(): Flip all the keys & values.
Example:
<?php
$arr = array('a'=>'A', 'b'=>'B', 'c'=>'C');
$flip_this = array_flip($arr);
foreach($flip_this as $key=>$value){
echo 'Key:' . $key . ' and Value:' . $value . "\n";
}
?>
Result:
Key:A and Value:a
Key:B and Value:b
Key:C and Value:c
If you friends wanted to discuss here anything related to PHP functions and tips, please share those things.
No comments:
Post a Comment