PHP Text to Slug Converter
How to Create a Text to Slug Converter in Vanilla PHP
Watch the full tutorial here:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="./backend.php" method="post">
<input type="text" name="txt">
<button type="submit">convert text</button>
</form>
</body>
</html>
php
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$txt = $_POST['txt'];
$txt = strtolower($txt);
$txt = str_replace(" ", "-", $txt);
$txtSlug = preg_replace('/[^a-z0-9\-]/', '', $txt);
echo "No slug: " . $_POST['txt'];
echo "<br>";
echo "Slug: $txtSlug";
}