前提・実現したいこと
preg_match関数を用いて、正規表現を作成
・西暦
・電話番号
・フォームタグ
・メールアドレス
・URL
発生している問題・エラーメッセージ
西暦と電話番号の箇所だけ試しましたが、
preg_match関数の使い方が間違っているのか
エラーが発生しております。
Warning: preg_match() expects at least 2 parameters, 1 given in /home/codecamp21605/htdocs/php/17/challenge_preg_match.php on line 3 Warning: preg_match() expects at least 2 parameters, 1 given in /home/codecamp21605/htdocs/php/17/challenge_preg_match.php on line 7 Warning: preg_match() [function.preg-match]: Empty regular expression in /home/codecamp21605/htdocs/php/17/challenge_preg_match.php on line 36 Warning: preg_match() [function.preg-match]: Empty regular expression in /home/codecamp21605/htdocs/php/17/challenge_preg_match.php on line 36 Warning: preg_match() [function.preg-match]: Empty regular expression in /home/codecamp21605/htdocs/php/17/challenge_preg_match.php on line 36 Warning: preg_match() [function.preg-match]: Empty regular expression in /home/codecamp21605/htdocs/php/17/challenge_preg_match.php on line 36 Warning: preg_match() [function.preg-match]: Empty regular expression in /home/codecamp21605/htdocs/php/17/challenge_preg_match.php on line 36 西暦の正規表現チェック: 不一致: 1953 不一致: 2013 電話番号の正規表現チェック: 不一致: 03-1111-1111 不一致: 040-222-2222 不一致: 0120-000-000 fromタグの正規表現チェック: // 部分一致: <form> 【一致した文字列: 】 部分一致: <form method="post"> 【一致した文字列: 】 メールアドレスの正規表現チェック: // 部分一致: test@test.com 【一致した文字列: 】 部分一致: test_2@test.co.jp 【一致した文字列: 】 部分一致: test.3@example.ne.jp 【一致した文字列: 】 URLの正規表現チェック: // 部分一致: http://codecamp.jp 【一致した文字列: 】 部分一致: https://test.com 【一致した文字列: 】 部分一致: http://codecamp.jp/index.html?q=test 【一致した文字列: 】
該当のソースコード
php
1<?php 2// 西暦 3$regexp_year = preg_match('/^[12][0-9]{3}$/'); // 西暦の正規表現を入力 4$check_year[0] = '1953'; 5$check_year[1] = '2013'; 6// 電話番号 7$regexp_phone_number = preg_match('/^[0-9]{2,3,4}-[0-9]{3,4}-[0-9]{3,4}$/'); // 電話番号の正規表現を入力 8$check_phone_number[0] = '03-1111-1111'; 9$check_phone_number[1] = '040-222-2222'; 10$check_phone_number[2] = '0120-000-000'; 11// formタグ 12$regexp_form = '//'; // formの正規表現を入力 13$check_form[0] = '<form>'; 14$check_form[1] = '<form method="post">'; 15// メールアドレス 16$regexp_mail = '//'; // メールアドレスの正規表現を入力 17$check_mail[0] = 'test@test.com'; 18$check_mail[1] = 'test_2@test.co.jp'; 19$check_mail[2] = 'test.3@example.ne.jp'; 20// URL 21$regexp_url = '//'; // URLの正規表現を入力 22$check_url[0] = 'http://codecamp.jp'; 23$check_url[1] = 'https://test.com'; 24$check_url[2] = 'http://codecamp.jp/index.html?q=test'; 25//////////////////////////////////////////////////////// 26// これ以降の処理はソースコード変更を行わないようにしてください。 27//////////////////////////////////////////////////////// 28$reslut_check_phone_number = check_regexp($regexp_phone_number, $check_phone_number); 29$reslut_check_form = check_regexp($regexp_form, $check_form); 30$reslut_check_mail = check_regexp($regexp_mail, $check_mail); 31$reslut_check_year = check_regexp($regexp_year, $check_year); 32$reslut_check_url = check_regexp($regexp_url, $check_url); 33function check_regexp($regexp, $str_data) { 34 $msg = array(); 35 foreach ($str_data as $value) { 36 if (preg_match($regexp, $value, $macthes) === 1) { 37 if ($value === $macthes[0]) { 38 $msg[] = '完全一致: ' . $value; 39 } else { 40 $msg[] = '部分一致: ' . $value . ' 【一致した文字列: ' . $macthes[0] . '】'; 41 } 42 } else { 43 $msg[] = '不一致: ' . $value; 44 } 45 } 46 return $msg; 47} 48?> 49<!DOCTYPE html> 50<html lang="ja"> 51<head> 52 <meta charset="UTF-8"> 53 <title>正規表現課題</title> 54 <style type="text/css"> 55 h1 { 56 font-size: 16px; 57 } 58 h1, p { 59 margin: 0px; 60 } 61 </style> 62</head> 63<body> 64 <section> 65 <h1>西暦の正規表現チェック: <?php print htmlspecialchars($regexp_year, ENT_QUOTES, 'UTF-8'); ?></h1> 66<?php foreach ($reslut_check_year as $value) { ?> 67 <p><?php print htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); ?></p> 68<?php } ?> 69 </section> 70 <section> 71 <h1>電話番号の正規表現チェック: <?php print htmlspecialchars($regexp_phone_number, ENT_QUOTES, 'UTF-8'); ?></h1> 72<?php foreach ($reslut_check_phone_number as $value) { ?> 73 <p><?php print htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); ?></p> 74<?php } ?> 75 </section> 76 <section> 77 <h1>fromタグの正規表現チェック: <?php print htmlspecialchars($regexp_form, ENT_QUOTES, 'UTF-8'); ?></h1> 78<?php foreach ($reslut_check_form as $value) { ?> 79 <p><?php print htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); ?></p> 80<?php } ?> 81 </section> 82 <section> 83 <h1>メールアドレスの正規表現チェック: <?php print htmlspecialchars($regexp_mail, ENT_QUOTES, 'UTF-8'); ?></h1> 84<?php foreach ($reslut_check_mail as $value) { ?> 85 <p><?php print htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); ?></p> 86<?php } ?> 87 </section> 88 <section> 89 <h1>URLの正規表現チェック: <?php print htmlspecialchars($regexp_url, ENT_QUOTES, 'UTF-8'); ?></h1> 90<?php foreach ($reslut_check_url as $value) { ?> 91 <p><?php print htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); ?></p> 92<?php } ?> 93 </section> 94</body> 95</html>
試したこと
西暦と電話番号の箇所だけ試したのですが、
preg_match関数の使い方が間違っているのか
エラーが発生しております。
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/17 02:59