I spent a while replacing all my ereg() calls to preg_match(), since ereg() is now deprecated and will not be supported as of v 6.0.
Just a warning regarding the conversion, the two functions behave very similarly, but not exactly alike. Obviously, you will need to delimit your pattern with '/' or '|' characters.
The difference that stumped me was that preg_replace overwrites the $matches array regardless if a match was found. If no match was found, $matches is simply empty.
ereg(), however, would leave $matches alone if a match was not found. In my code, I had repeated calls to ereg, and was populating $matches with each match. I was only interested in the last match. However, with preg_match, if the very last call to the function did not result in a match, the $matches array would be overwritten with a blank value.
Here is an example code snippet to illustrate:
$test = array('yes','no','yes','no','yes','no');
foreach ($test as $key=>$value) {
ereg("yes",$value,$matches1);
preg_match("|yes|",$value,$matches2);
}
print "ereg result: $matches1[0]
";
print "preg_match result: $matches2[0]
";
?>
The output is:
ereg result: yes
preg_match result:
($matches2[0] in this case is empty)
I believe the preg_match behavior is cleaner. I just thought I would report this to hopefully save others some time.
#$1', $str);
Keterangan:
* Pola setelah # adalah ([-\w]+) atau sama dengan ([-A-Za-z0-9_]+).
Jika ingin membatasi panjang tagar, bisa ubah + menjadi misal {1,20}. Jika melebihi 20 karakter, hanya 20 karakter pertama yang akan diubah menjadi link.
* Pola sebelum # adalah (? ?negative lookbehind, diikuti dengan & (ampersand) dan ; (semicolon).
Menggunakan karakter & dan ; agar string dengan character reference di HTML seperti ' ' tidak akan ikut diubah menjadi link tagar.
Untuk membuat teks mention menjadi link, tinggal ubah # menjadi @ dan sesuaikan link.
#PHP#regex ###PHP #regex
Baca juga: Cara Membuat Sistem Hashtag dan Mention di PHP tanpa regex
Kode di bawah ini menunjukkan cara sederhana untuk memeriksa apakah kolom nama hanya berisi huruf dan spasi. Jika nilai dari kolom nama tidak valid, maka menyimpan pesan kesalahan:
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
PHP - Validasi E-mail
Cara termudah dan paling aman untuk memeriksa apakah alamat email dengan baik terbentuk adalah dengan menggunakan filter_var () fungsi PHP.
Dalam kode di bawah, jika alamat e-mail tidak well-formed, kemudian menyimpan pesan kesalahan:
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
PHP - Validasi URL
Kode di bawah ini menunjukkan cara untuk memeriksa apakah sintaks alamat URL berlaku (this regular expression also allows dashes in the URL) . Jika sintaks alamat URL tidak valid, maka menyimpan pesan kesalahan:
$website = test_input($_POST["website"]) ;
if (! preg_match("/\b(?:(?:https?|ftp) :\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
PHP - Validasi Nama, E-mail, dan URL
Sekarang, script terlihat seperti ini:
Contoh
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}
?>
Langkah berikutnya adalah untuk menunjukkan bagaimana mencegah formulir dari mengosongkan semua field input ketika pengguna menyerahkan formulir.