本文主要内容:
详解http_build_query函数的用法
http_build_query 函数 (PHP 5)
http_build_query -- 生成 url-encoded 之后的请求字符串描述string http_build_query ( array formdata [, string numeric_prefix] )
使用给出的关联(或下标)数组生成一个 url-encoded 请求字符串。参数 formdata 可以是数组或包含属性的对象。一个 formdata 数组可以是简单的一维结构,也可以是由数组组成的数组(其依次可以包含其它数组)。
如果在基础数组中使用了数字下标同时给出了 numeric_prefix 参数,此参数值将会作为基础数组中的数字下标元素的前缀。这是为了让 PHP 或其它 CGI 程序在稍后对数据进行解码时获取合法的变量名。
例1. http_build_query()
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data);
/* 输出:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
*/
?>
例2. http_build_query() 使用数字下标的元素
$data = array('foo', 'bar', 'baz', 'boom', 'cow' => 'milk', 'php' =>'hypertext processor');
echo http_build_query($data);
/* 输出:
0=foo&1=bar&2=baz&3=boom&cow=milk&php=hypertext+processor
*/
echo http_build_query($data, 'myvar_');
/* 输出:
myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk&php=hypertext+processor
*/
?>
例3. http_build_query() 使用复杂的数组
$data = array('user'=>array('name'=>'Bob Smith',
'age'=>47,
'sex'=>'M',
'dob'=>'5/12/1956'),
'pastimes'=>array('golf', 'opera', 'poker', 'rap'),
'children'=>array('bobby'=>array('age'=>12,
'sex'=>'M'),
'sally'=>array('age'=>8,
'sex'=>'F')),
'CEO');
echo http_build_query($data, 'flags_');
/* 输出:(为了可读性对其进行了折行)
user[name]=Bob+Smith&user[age]=47&user[sex]=M&user[dob]=5%1F12%1F1956&
pastimes[0]=golf&pastimes[1]=opera&pastimes[2]=poker&pastimes[3]=rap&
children[bobby][age]=12&children[bobby][sex]=M&children[sally][age]=8&
children[sally][sex]=F&flags_0=CEO //www.
注意:只有基础数组中的数字下标元素“CEO”才获取了前缀,其它数字下标元素(如
pastimes 下的元素)则不需要为了合法的变量名而加上前缀。
*/
?>
例4. http_build_query() 使用对象
class myClass {
var $foo;
var $baz;
function myClass() {
$this->foo = 'bar';
$this->baz = 'boom';
}
}
$data = new myClass();
echo http_build_query($data);
/* 输出:
foo=bar&baz=boom
*/
?>
本文主要内容:
根据checkbox复选框选择多个内容进行删除操作,学习下php删除一组文件的方法。
1,静态页 form.html
<form action="/blog_article/process.html" method="post">
<input type="checkbox" value="file1.html" name="delete[]"> file1.html<br>
<input type="checkbox" value="file2.html" name="delete[]"> file2.html<br>
<input type="checkbox" value="file3.html" name="delete[]"> file3.html<br>
<input type="submit" value="Delete Selected" name="submit">
</form>
2,删除一组文件的代码 process.php
# Take each checked value, and give it a name of $delete
foreach($_POST['delete'] as $delete){
# Make sure the file actually exists
if(file_exists("/location/of/directory/".$delete)){
# Delete each file
unlink("/location/of/directory/",$delete);
}else{
continue;
}
}
# Return to form.html
header("Location: form.html");
?>
您可能感兴趣的文章:
PHP删除N分钟前创建的所有文件的小例子
PHP实例:批量删除文件夹及文件夹中的文件
删除指定文件夹中所有文件的php代码
删除多级目录的php自定义函数
php递归删除文件与目录的代码
php递归删除目录及多级子目录下所有文件的代码
php递归创建和删除文件夹的代码
php递归删除目录的例子
本节主要内容:
php实现数字转换为罗马字符的代码。
例子:
<?PHP
/*
Decimal <--> Roman 1.1
用到了二个函数
string dec2roman (integer $number)
where $number is a number you want to convert to a roman one.
integer roman2dec (string $linje)
where $linje is the roman number you want to conver to an integer.
edit: www.
*/
function dec2roman ($number) {
# Making input compatible with script.
$number = floor($number);
if($number < 0) {
$linje = "-";
$number = abs($number);
}
# Defining arrays
$romanNumbers = array(1000, 500, 100, 50, 10, 5, 1);
$romanLettersToNumbers = array("M" => 1000, "D" => 500, "C" => 100, "L" => 50, "X" => 10, "V" => 5, "I" => 1);
$romanLetters = array_keys()($romanLettersToNumbers);
# Looping through and adding letters.
while ($number) {
for($pos = 0; $pos <= 6; $pos++) {
# Dividing the remaining number with one of the roman numbers.
$dividend = $number / $romanNumbers[$pos];
# If that division is >= 1, round down, and add that number of letters to the string.
if($dividend >= 1) {
$linje .= str_repeat($romanLetters[$pos], floor($dividend));
# Reduce the number to reflect what is left to make roman of.
$number -= floor($dividend) * $romanNumbers[$pos];
}
}
}
# If I find 4 instances of the same letter, this should be done in a different way.
# Then, subtract instead of adding (smaller number in front of larger).
$numberOfChanges = 1;
while($numberOfChanges) {
$numberOfChanges = 0;
for($start = 0; $start < strlen($linje); $start++) {
$chunk = substr($linje, $start, 1);
if($chunk == $oldChunk && $chunk != "M") {
$appearance++;
} else {
$oldChunk = $chunk;
$appearance = 1;
}
# Was there found 4 instances.
if($appearance == 4) {
$firstLetter = substr($linje, $start - 4, 1);
$letter = $chunk;
$sum = $firstNumber + $letterNumber * 4;
$pos = array_search($letter, $romanLetters);
# Are the four digits to be calculated together with the one before? (Example yes: VIIII = IX Example no: MIIII = MIV
# This is found by checking if the digit before the first of the four instances is the one which is before the digits in the order
# of the roman number. I.e. MDCLXVI.
if($romanLetters[$pos - 1] == $firstLetter) {
$oldString = $firstLetter . str_repeat($letter, 4);
$newString = $letter . $romanLetters[$pos - 2];
} else {
$oldString = str_repeat($letter, 4);
$newString = $letter . $romanLetters[$pos - 1];
}
$numberOfChanges++;
$linje = str_replace()($oldString, $newString, $linje);
}
}
}
return $linje;
}
function roman2dec ($linje) {
# Fixing variable so it follows my convention
$linje = strtoupper()($linje);
# Removing all not-roman letters
$linje = ereg_replace("[^IVXLCDM]", "", $linje);
print("\$linje = $linje<br>");
# Defining variables
$romanLettersToNumbers = array("M" => 1000, "D" => 500, "C" => 100, "L" => 50, "X" => 10, "V" => 5, "I" => 1);
$oldChunk = 1001;
# Looping through line
for($start = 0; $start < strlen($linje); $start++) {
$chunk = substr($linje, $start, 1);
$chunk = $romanLettersToNumbers[$chunk];
if($chunk <= $oldChunk) {
$calculation .= " + $chunk";
} else {
$calculation .= " + " . ($chunk - (2 * $oldChunk));
}
$oldChunk = $chunk;
}
# Summing it up
eval("\$calculation = $calculation;");
return $calculation;
}
# Implementation of the array_search function. Works only with numerical arrays.
function array_search($searchString, $array) {
foreach ($array as $content) {
if($content == $searchString) {
return $pos;
}
$pos++;
}
}
?>
您可能感兴趣的文章:
php数字转换为指定长度字符串的函数php全角数字转半角数字的代码一例
php实现IP地址与数字互换的代码
php数字格式化的例子(number_format函数应用)
php 数字格式化函数 number_format 简介
阿拉伯数字转中文数字的php自定义函数
人民币金额数字转中文大写的php函数
货币数字转换为中文大写的php代码
php批量获取首字母(汉字、数字、英文)的代码
将IP地址和数字相互转化的php代码