###### tags: `資料庫程式設計` `Javascript` > 有什麼新方法都可以放上來喔 > 我只是放我通過的程式碼而已 # OnlineTest - PHP 單元 1 ## 1. Echo HTML :::info 回傳一個字串"\<div>hello world\</div>" ```php= <?php echo '<div>hello world</div>'; ?> ``` ::: ## 2. Echo :::info 將輸入的內容重新輸出出來 **Input:** abcdefg **Output:** abcdefg ```php= <?php echo(readline()); ?> ``` ::: ## 3. 數字加總 :::info 輸入多個數字,輸出所有數字的加總 **Input:** 1 100 20 30 **Output:** 151 ```php= <?php $nums = explode(' ', readline()); $sum = 0; foreach ($nums as $num) { $sum += (int) $num; } echo $sum; ?> ``` ::: ## 4. 邏輯運算子 :::info  ```php= <?php $arr = explode(' ', readline()); $check = false; if(((int)$arr[0] and (int)$arr[1]) == $arr[2]){ echo "AND\n"; // this new line need to use \n not <br/> $check = true; } if(((int)$arr[0] or (int)$arr[1]) == $arr[2]){ echo "OR\n"; $check = true; } if(((int)$arr[0] xor (int)$arr[1]) == $arr[2]){ echo "XOR\n"; $check = true; } if(!$check){ echo "IMPOSSIBLE\n"; } ?> ``` 使用`array_map('要執行的function','被處理的array')` ```php= <?php $input = array_map('intval', explode(' ', readline())); $output = false; if (($input[0] and $input[1]) == $input[2]) { echo "AND\n"; $output = true; } if (($input[0] or $input[1]) == $input[2]) { echo "OR\n"; $output = true; } if (($input[0] xor $input[1]) == $input[2]) { echo "XOR\n"; $output = true; } if (!$output) echo "IMPOSSIBLE\n"; ?> ``` ::: ## 5. For迴圈 :::info 依據輸入的數量輸出div **Inout:** 4 **Output:** \<div>1\</div> \<div>2\</div> \<div>3\</div> \<div>4\</div> ```php= <?php $num = readline(); for ($i=1; $i<=$num; $i++) { echo "<div>$i</div>\n"; } ?> ``` ::: ## 6. 質數判斷 :::info 依據輸入的數字判斷是否為質數,是的話輸出"YES",不是的話輸出"NO" **Input:** 101 **Output:** YES ```php= <?php function isprime($num) { for ($i=2; $i*$i<=$num; $i++) { if ($num % $i == 0) return 'NO'; } return 'YES'; } $num = readline(); echo isprime($num); ?> ``` ::: ## 7. 判斷密碼安全性 :::info 輸入第一行表示有n個密碼,密碼長度必須至少8,包含大小寫的英文和數字,符合時輸出"Y",不符合時輸出"N" **Input:** 3 abcdefgh aabbccddEE11 abc **Output:** NYN ```php= <?php $n = (int) readline(); while ($n--) { $pw = readline(); if (preg_match('/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,}$/', $pw)) echo 'Y'; else echo 'N'; } ?> ``` ::: ## 8. 擷取文章簡介 :::info 輸入一篇文章,當長度超過30個字時只輸出前30個字並加上"...",沒超過20個字直接輸出文章,簡介需放在\<p>\</p>裡面 **Input:** Russian forces bombarded Kyiv and Chernihiv Wednesday. **Output:** \<p>Russian forces bombarded Kyiv ...\</p> ```php= <?php $str = readline(); $len = strlen($str); if($len >= 30){ $str = substr($str, 0, 30)."..."; } echo "<p>$str</p>"; ?> ``` ::: ## 9. 時間戳 :::info 輸入一個ISO 8601的時間戳,輸出特定格式的時間(時區為Asia/Taipei) 輸出格式範例:"2019/01/01 01:01:00" **Input:** 2020-05-03T17:30:08+00:00 **Output:** 2020/05/04 01:30:08 ```php= <?php $timestamp = readline(); $day = date_create($timestamp); $day = date_timezone_set($day, new DateTimeZone('Asia/Taipei')); echo date_format($day, 'Y/m/d H:i:s'); ?> ``` ::: ## 10. 擷取HTML的文字 :::info 輸入一段html,需輸出其中的文字部分 **Input:** \<div>Hello\<p>world\</p>\</div> **Output:** Helloworld 提示:strip_tags ```php= <?php $html = readline(); echo strip_tags($html); ?> ``` :::
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up