標題:
[SQL與PHP]
PHP分割textarea每一行的數據
[打印本頁]
作者:
gswdkimo
時間:
2011-6-24 17:03
標題:
PHP分割textarea每一行的數據
<form id="textarea_test" action="textarea.php" method="post">
<textarea cols="50" rows="5" name="aa" id="abc" >
</form>
複製代碼
<?php
$str = $_POST['aa'];
$array = explode("\n", trim($str));
var_dump($array);
?>
複製代碼
作者:
gswdkimo
時間:
2011-6-24 17:07
標題:
應用範例
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>SQL語法產生器[權限]</title>
</head>
<body>
<form name="form" method="post" action="<?php echo basename(getenv('SCRIPT_NAME')); ?>">
id代碼:<input name="id" type="text"> <br>
page代碼:<textarea cols="4" rows="6" name="texta"></textarea> <br>
<input name="button" value="確定" type="submit">
<input type=hidden name='work' value='yes'>
</form>
<?php
//-----------------------------------判斷是否產生語法開始-----------------------------------
if ((isset($_POST["work"])) && ($_POST["work"] == "yes")) {
$id1=$_POST['id'];
$texta1=$_POST['texta'];
if(trim($id1)==null){
echo'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language=JavaScript>
alert("請輸入id代碼!");
//先以?字元取出目前不含參數的url,再以/字元拆解並存至LSA陣列
var LSA = location.href.split("?")[0].split("/");
//取出陣列的最後一項即為目前檔名
var CFN = LSA[LSA.length-1];
window.location.href=CFN;//轉向該網頁
</script>';
}
//排除只輸入空白字元
if(trim($texta1)==null){
echo'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language=JavaScript>
alert("請輸入page代碼!");
//先以?字元取出目前不含參數的url,再以/字元拆解並存至LSA陣列
var LSA = location.href.split("?")[0].split("/");
//取出陣列的最後一項即為目前檔名
var CFN = LSA[LSA.length-1];
window.location.href=CFN;//轉向該網頁
</script>';
}
$texta1_array=explode("\n",$texta1);
echo '<textarea cols="60" rows="5" name="texta">//新增語法'."\n";
foreach($texta1_array as $page){ //foreach:給一個陣列,來取出陣列的值
if(trim($page)!=null){
echo 'replace into group(`id`, `page`) values(\''.$id.'\',\''.trim($page).'\');'."\n";
}
}
echo '</textarea>';
echo '<textarea cols="60" rows="5" name="texta">//刪除語法'."\n";
foreach($texta1_array as $page){
if(trim($page)!=null){
echo 'delete from group where id=\''.$id.'\', page=\''.trim($page).'\';'."\n";
}
}
echo '</textarea>';
}
//-----------------------------------判斷是否產生語法結束-----------------------------------
?>
</body>
</html>
複製代碼
歡迎光臨 資訊家電班NO7 (http://imce07.freehostia.com/forums/)
Powered by Discuz! 7.2