2

I am trying to remove the last two commmas of an array using the trim function but can't get it to work....Is my syntax correct?

I have tried to use the trim function after imploding the array into a string

if(isset($_POST['doctors'])) {
$doctors = $_POST['doctors'];
$doctors = implode(', ', $doctors);
$doctors = strip_tags($doctors);
$doctors = trim($doctors);
} else {
  $doctors = 'Not Supplied';
}

I also used rtrim but still can't remove the last commas... I will also include my form html, just in case I did something wrong there:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
 <div class="medical_form">
    <div class="medical_form_title">
    <h2>Submit a Review of a Medical Clinic</h2>
 </div>
   <form class="medical_form" action="medical_review_process.php" method="POST">
    <br />
    <label>Name of Medical Clinic:</label>
    <br />
    <br />
    <input type="text" name="clinic_name" >
    <br />
    <br />
    <label>Address of the Clinic:</label>
    <br />
    <br />
    <input type="text" name="clinic_address" >
    <br />
    <br />
    <label>Phone number of the Clinic:</label>
    <br />
    <br />
    <input type="text" name="clinic_number" >
    <br />
    <br />
    <label>Are you a healthcare professional or a patient?</label>
    <br />
    <br />
    <input type="radio" name="type_of_profession" value="healthcare">Healthcare
    <br />
    <br />
    <input type="radio" name="type_of_profession" value="patient">Patient
    <br />
    <br />
    <label>Which of the following services does your clinic provide?</label>
    <br />
    <br />
    <input type="checkbox" name="services[]" value="skin_cancer">Skin cancer check-ups<br />
    <input type="checkbox" name="services[]" value="pregnancy">Pregnancy/antenatal check-ups<br>
    <input type="checkbox" name="services[]" value="pap_smears">Pap smears<br>
    <input type="checkbox" name="services[]" value="immunisations">Immunisations<br>
    <input type="checkbox" name="services[]" value="ear_syringe">Ear syringe<br>
    <input type="checkbox" name="services[]" value="ear_irrigation">Ear irrigation<br>
    <input type="checkbox" name="services[]" value="ear_suctioning">Ear suctioning<br>
    <input type="checkbox" name="services[]" value="eye_check-ups">Eye check-ups<br>
    <input type="checkbox" name="services[]" value="blood test">Blood test<br>
    <br />
    <br />
    <label>Other Services:</label>
    <br />
    <br />
    <input type="text" name="others" >
    <br />
    <br />
    <label>Recommended Doctors:</label>
    <br />
    <br />
    <input type="text" name="doctors[]" >
    <br />
    <br />
    <label>Recommended Doctors(2):</label>
    <br />
    <br />
    <input type="text" name="doctors[]" >
    <br />
    <br />
    <label>Recommended Doctors(3):</label>
    <br />
    <br />
    <input type="text" name="doctors[]" >
    <br />
    <br />
    <label>Upload pictures here to support information</label>
    <br />
    <br />
    <input type="text" name="pictures" >
    <br />
    <br />
    <button type="submit" name="submit">Register</button>
   </form>
</div>
</body>
</html>

I have 3 doctors input but users can only type just one and if they typed just one doctor, I would like to remove the last two commas:

I still have the following image:

enter image description here

piano0011
  • 61
  • 7
  • 1
    You seem to have a lot of unanswered questions (or not marked as answered anyway). It could be worth going through some of them and clearing them up (https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Nigel Ren Dec 23 '18 at 08:05
  • On a side note: You shouldn't save comma separated values in a column, it's difficult to deal with, see https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad – Progman Dec 23 '18 at 10:10

2 Answers2

1

There are two possibilities as far as I can see.

trim() only removes spaces, so to trim commas as well you need to add what characters it should trim (commas and spaces in this example)...

$doctors = trim($doctors, ", ");

Or you could remove empty entries from the array before implodeing it using array_filter()...

$doctors = array_filter($_POST['doctors']);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • I tried the array_filter method but still got the last two commas appearing in my database as shown in my picture – piano0011 Dec 23 '18 at 08:17
  • Did you try the `trim()` with the comma and the space? – Nigel Ren Dec 23 '18 at 08:18
  • Actually, the array_filter works like a charm! So why can't I use trim to remove the empty entries? – piano0011 Dec 23 '18 at 08:20
  • When you implode - you use a comma and a space, so you need to trim both of these off. – Nigel Ren Dec 23 '18 at 08:20
  • I don't know why the trim() with the comma did not work but the array_filter works – piano0011 Dec 23 '18 at 08:21
  • I guess they both serve a different function and I should use both because the trim will also get rid of any extra commas that the user might type it – piano0011 Dec 23 '18 at 08:33
  • Just to add to my last comment, is it common practice to use trim for any type of form? I guess you wouldn't know what the user might type in a form... they could type any other symbols..... – piano0011 Dec 23 '18 at 08:36
  • I think in general it is difficult to know if extra space or other characters are intended or not. Some things like names though have usual common things and extra spaces can cause confusion when searching for data, `Mervin ` is not the same as `Mervin` but they look exactly the same. – Nigel Ren Dec 23 '18 at 08:39
1

You can try it

rtrim(',',$doctors);