-1

I am trying to make it where submit button is pressed and ALL inputs are only left with letters and numbers and then echoed out onto a page. I'm not getting any errors but it still won't print out formatted text. I can echo out a 3 lined command but when converting it to an input I get lost.

Can anyone help me get the code to act the way I want it? I'm not trying to sanitize data or insert into a database. I'm simply trying to remove unreadable words onto a page due to fat fingers, drunks or whatever

I've tried

preg_replace('/^[a-zA-Z0-9]+/', $name)
preg_replace('/^[a-zA-Z0-9]+/', $_POST["name"])
<html>
<head>
<title>test</title>
</head>
<body>
<form id="form" class="appnitro"  method="post" action="">
<h2>stripping extra crap</h2>               
<div>
<br>

name<br>
<input id="element_1" name="name" type="text" maxlength="20" value="test$%+=-?"/> <br>
test1<br>
<input id="element_1" name="test1" type="text" maxlength="20" value="test$%+=-?"/> <br>
test2<br>
<input id="element_1" name="test2" type="text" maxlength="20" value="test$%+=-?"/> <br>
test3<br>
<input id="element_1" name="test3" type="text" maxlength="20" value="test$%+=-?"/> <br>
</div>  
<input id="" class="button_text" type="submit" name="" value="Submit" />
</form> 
</div>
</body>
</html>


<?php

if (empty($name) && empty($test1) && empty($test2) && empty($test3)) {
    echo 'Please fill in the fields';
    return false;

}


if (isset($_POST['submit'])){


    implode("", $_POST);

    preg_replace('/^[a-zA-Z0-9]+/', $_POST);//testing string to replace $name to letters and numbers only


}
echo 'name with only letters and numbers?<br>';                    
echo $name;
 echo '<br>'; 
  echo '<br>'; 
echo 'is post array still an array or string after implode?<br>';                    
echo $_POST;
echo '<br>'; 
 echo '<br>'; 
echo 'test1 with only letters and numbers?<br>';                 
echo $test1;
echo '<br>';  

?>
--------------------------------
<?php
foreach($_POST as $key=>$value)
{
  echo "<br>$key=$value<br>";
}

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);


?>
Vladimir Vlasov
  • 1,860
  • 3
  • 25
  • 38

1 Answers1

0

'preg_replace' requires 3 parameters (see documentation).

So in your case it should be: $newName = preg_replace('/[^a-zA-Z0-9]+/', '', $name);

Moreover, I moved the '^' because:

  • at its old position, it meant 'match this regex at the beginning of the text'.

  • at its new position, it means 'anything but [a-zA-Z0-9]'

So now the function replaces all non-alpha-characters by null (you can read Johan Sjöberg's answer on Regex not operator to have more info).

proprit
  • 938
  • 7
  • 13
  • That did not do anything $%+=-? is still there after pressing the submit button – tom tucker Dec 07 '18 at 17:40
  • Have you noticed the '^' has moved? I did an edit to explain – proprit Dec 07 '18 at 17:41
  • It should work, here is an example: http://sandbox.onlinephpfunctions.com/code/00f4327e95d18a429050f476323847021e03c69a – proprit Dec 07 '18 at 17:46
  • You should make use of the result too (as preg_replace does not modify your entry): $newName = preg_replace('/[^a-zA-Z0-9]+/', '', $name); – proprit Dec 07 '18 at 17:49