PHP arrays, explode, substr and trim - quick through

Array is kind of data structure where it stores multiple values in a single variable. you can save multiple values like in a stack by using arrays. define array in php as follows.


$arrayExample = array( value1,value2,value3);
example:

$x = array(10,20,30);

Insert data to array


$x=array();//empty array
array_push($x,1,2);
print_r($x);
output: Array ( [0] => 1 [1] => 2 )

Read from array


$numberArray = array( 10, 20, 30);
foreach( numberArray as $readvalue ) {
    print_r($readvalue);
}

explode

The function explode use to chunk/break string to an array .

$string = "number1,number2,number3";
$expldedArray = explode(",",$string);
the above example, $explodedArray has 3 elements where $string breaks by comma.

Trim

trim function allows you to remove given string or character from both sides of strings, there is another two functions called ltrim and rtrim which allows you to remove content from the exact end.

$string = “tec.icrony.net“;
echo trim($string,“tec..net“);//remove from both sides and returns icrony
echo ltrim($string,“tec.“);//returns icrony.net
echo rtrim($string,“.net“);//returns tec.icrony

Sub String

substr() can used to return a part of a string.This is very useful when you trying to extract a part of string .

$string = “tec.icrony.net“;
echo substr($string,4,6);//return icrony
for more you can refer here

Rasika Preethiraj

A technophile at icrony since 2012, In a quest to programmatic automations for scaled organisations through the use of python, php, autoit and machine learning


Comments


Post a Comment



Trending

Latest Posts

Tags

Newsletter

Subscribe to our newsletter for good news, sent out every month.