PHP 常用代码目录
文章目录
1. 提交PHP数据
<form action="" method="post" name="form1"> <textarea cols="50" rows="8" name="group_name">放入批量关键词</textarea> <input type="submit" name="submit" value="提交"/> <br> </form> <?php echo rand(1000000, 9999999); // 随机数字 echo rand(10, 100); if (isset($_POST['submit'])) { $data = $_POST['group_name']; $ci = explode("\r\n", $data); // 把字符串打散为数组 $count = count($ci); $cia = file('http://mm.77wd.cn/词/货币/cb.txt'); // 函数把整个文件读入一个数组中 // 假设存在变量 $cib, $cic, $cie, $cif,以下代码需要这些变量的定义 $bss = array_merge($cia, $cib, $cic, $cie, $cif); // 多个数组的单元合并起来 $bss = array_unique($bss); // 移除数组中的重复值,只保留第一个值 shuffle($bss); // 随机顺序重新排列 $bi = mb_strlen($data); // 字符串长度 if ($bi > 4) { echo $data; } } ?>
2. 删除空格
function trimall($str) { $qian = array(" ", " ", "\t", "\n", "\r"); $hou = array("", "", "", "", ""); return str_replace($qian, $hou, $str); }
3. 数组打印
$arr_test = array(1, 2, 3); echo '<pre>'; print_r($arr_test); echo '</pre>'; var_dump($arr_test);
4. 数组去重
$arr = [12, 1, 12, 1, 456, 789]; $result = array_unique($arr); echo '<pre>'; print_r($result); echo '</pre>';
5. 数组合并
$a1 = array("red", "green"); $a2 = array("blue", "yellow"); $bss = array_merge($a1, $a2); echo '<pre>'; print_r($bss); echo '</pre>';
6. 数组随机排序
$array = array('A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'); shuffle($array); // 随机排序数组 $shu = count($array); echo "数组个数: $shu"; echo '<pre>'; print_r($array); echo '</pre>';
7. 常规数组
$cars = array("Volvo", "BMW", "常规数组"); echo "常规数组:I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; echo '<br><br>'; // 人工分配 ID 键:$cars[0] = "Volvo";
8. 关联数组
$age = array("Peter" => "关联数组35", "Ben" => "37", "Joe" => "43"); echo "关联数组:Peter is " . $age['Peter'] . " years old."; echo '<br><br>'; foreach ($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; }
9. 常量
define("GREETING", "常量欢迎访问 Runoob.com"); echo GREETING; // 输出 "欢迎访问 Runoob.com"
10. 函数
function writeName($name = "Kai Jim Refsnes") { echo "<br>$name<br>"; } echo "<br><br>函数My name is <br>"; writeName(); // 调用时没有传递参数,使用默认值 writeName("John Doe"); // 调用时传递了参数 "John Doe"
11,输出内容,不被转义用函数
使用 htmlspecialchars
输出内容,确保不转义引号
$text = "This is <b>bold</b> and this is <i>italic</i> text."; echo htmlspecialchars($text, ENT_NOQUOTES);
使用 htmlentities
输出内容,确保不转义引号
$text = "This is <b>bold</b> and this is <i>italic</i> text."; echo htmlentities($text, ENT_NOQUOTES);
示例代码
function outputText($text) { // 默认使用 echo 输出,不转义 echo $text; } $text = "This is <b>bold</b> and this is <i>italic</i> text."; echo "Using echo directly:<br>"; outputText($text); echo "<br><br>Using htmlspecialchars without escaping quotes:<br>"; echo htmlspecialchars($text, ENT_NOQUOTES); echo "<br><br>Using htmlentities without escaping quotes:<br>"; echo htmlentities($text, ENT_NOQUOTES);
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至123@#-@12-3.com举报,一经查实,本站将立刻删除。
共有 30 条评论