PHP/String/split

Материал из Web эксперт
Перейти к: навигация, поиск

Generating date-based drop-down menu options

 
<?php
list($hour, $minute, $second, $month, $day, $year) = split(":", date("h:i:s:m:d:Y"));
for ($i = 0; $i < 7; ++$i) {
    $timestamp = mktime($hour, $minute, $second, $month, $day + $i, $year); 
    $date = date("D, F j, Y", $timestamp);
    print "<option value="$timestamp">$date</option>\n";
}
?>



split() function divides a string into various elements.

 
Its syntax is: array split (string pattern, string string [, int limit])
<?
$ip = "123.456.789.000";      
$iparr = split ("\.", $ip);   
                              
print "$iparr[0] <br>";       
print "$iparr[1] <br>";       
print "$iparr[2] <br>";       
print "$iparr[3] <br>";       
?>



split.php

 
<?php
   $text = "this is\tsome text that\nwe might like to parse.";
   print_r(split("[\n\t]",$text));
?>



Use split() to limit a parameter to restrict division of $ip

 
<?
$ip = "123.456.789.000";      
$iparr = split ("\.", $ip, 2); 
                               
print "$iparr[0] <br>";        
print "$iparr[1] <br>";        
?>