Page 148 - Open Soource Technologies 304.indd
P. 148
Web Technologies-I
Notes [0] => zero
[1] => one
[2] => two
[3] => three
[4] => four
[5] => five
[6] => six
[7] => seven
[8] => eight
[9] => nine
)
The element number 5 is: five
In this example we have defined the variable $TheText, and whithin this variable we have
included several words separated by spaces.
In the next line, we have split the variable $TheText into an array named $Thearray.
Split command have been used to brake $TheText and “ “ (space) has been used as a delimiter
to separate the substrings.
In the response page we have printed the array by using command print_r, and element number
5 has been printed out.
It may happen to have a string we want to split, but we do not know how many substrings we
may get. In that case we may use command sizeof to discover how many elements are there
in our array, and then we may use that value to write them by using a foreach control structure
(see example below).
<pre>
<?
$TheText=”my dog is very nice and my cat is barking”;
$Thearray=split (“ “,$TheText) ;
?>
How many words have in $TheArray?
<? print sizeof ($Thearray); ?>
<?
Foreach ($Thearray as $key =>$val){
print “<br>Word number $key is $val”;
}
?>
</pre>
142 LOVELY PROFESSIONAL UNIVERSITY