Pablo Pablo - 4 months ago 22
PHP Question

php variable to make clean URL in a HTML form value that show the result

How I can make a html form that you put the information in a input an give you the result of the data in a clean URL in the next input, this is how far i get, the function works, i need that the first value (slugit) goes to the function, i leave the code, i will be happy if some one can help me:

<form action="test.php" method="post">
<input type="text" name="name"/> <!--put your first value (slugit) -->
<input type="submit" class="btn btn-success" value="Run">
</form>

<?php
setlocale(LC_ALL, 'en_US.UTF8');
function slugit($str, $replace=array(), $delimiter='-') {
if ( !empty($replace) ) {
$str = str_replace((array)$replace, ' ', $str);
}
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
$slug = slugit("esto es untést ed manejoñol132_sd. dsf-asd</");
echo $slug;
?>

<label>Result:</label>
<input type="text" name="slug_prod" id="slug_prod" value="<?php echo $slug; ?>">

Answer

hey try to use in this way

  <form action="test.php" method="post">              
  <input type="text" name="name" value="" />
  <input type="submit" class="btn btn-success" value="Run">
</form>

<?php
setlocale(LC_ALL, 'en_US.UTF8');
function slugit($str, $replace=array(), $delimiter='-') {
        $string = strtolower($str);
        //Strip any unwanted characters
        $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
        //Clean multiple dashes or whitespaces
        $string = preg_replace("/[\s-]+/", " ", $string);
        //Convert whitespaces and underscore to dash
        $string = preg_replace("/[\s_]/", "-", $string);
        return $string;
}
$test = slugit($_POST['name']);
$slug = $test;
?>

<label>Result:</label>
<input type="text" class="form-control" name="slug_prod" id="slug_prod" value="<?php echo $slug; ?>">