解析任意数量的分解数据


Parse through an arbitrary number of exploded data

基本上,我要求用户用逗号分隔他们的兴趣。我想解析这些内容,并将它们输入到SQL数据库中。有没有一种方法可以根据他们输入的兴趣数量任意解析?到目前为止,这是我的代码:

$interests = $_POST['interests'];
$interests = explode(',', $interests);
<?php
    $interests = $_POST['interests'];
    $interests = explode(',', $interests);
    foreach ($interests as $interest) {
        // INSERT INTO db using $interest. Make sure to properly escape each interest!
        // Use PDO and bind parameters to avoid sql injection attacks
    }
$interests = explode(',', str_replace(Array("'r", "'r"), ",", $_POST['interests'])); // the repalce here is to remove any new lines that might have been added if a textarea field used
$interests = array_filter($interests); // weeds out empty entries that might have been crated
foreach($interests AS $interest) {
  $interest = trim($interests); // remove extra spaces that might be
  // do what you need
}