Guest User

Untitled

a guest
Aug 10th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. Only process $_POST elements that begin with a certain string
  2. $_POST['city_1']
  3. $_POST['city_2']
  4. $_POST['city_3']
  5. $_POST['city_4']
  6.  
  7. <input type="text" name="cities[city_1]">
  8. <input type="text" name="cities[city_2]">
  9. <input type="text" name="cities[city_3]">
  10. <input type="text" name="cities[city_4]">
  11.  
  12. foreach($_POST['cities'] as $city)
  13. {
  14. echo $city;
  15. }
  16.  
  17. $cities = preg_grep('/^city_d+$/', array_keys($_POST));
  18.  
  19. foreach($cities as $city) {
  20. echo $_POST[$city];
  21. }
  22.  
  23. foreach($_POST as $name=>$value) {
  24. if (strpos($value, 'city_') !== 0) continue;
  25.  
  26. echo $value;
  27. }
  28.  
  29. foreach($_POST as $key => $value)
  30. if(preg_match("/^city_d+$/", $key))
  31. ...
  32.  
  33. function startsWith($haystack, $needle)
  34. {
  35. $length = strlen($needle);
  36. return (substr($haystack, 0, $length) === $needle);
  37. }
  38.  
  39.  
  40. foreach ($_POST as $k=>$v)
  41. {
  42. if (startsWith($k, 'city_')
  43. {
  44. // Process parameter here ...
  45. }
  46. }
  47.  
  48. foreach($_POST as $key=>$value) {
  49. //filter based on $key
  50. }
Add Comment
Please, Sign In to add comment