piano0011
piano0011

Reputation: 61

how to remove the last two commas in an array after using the implode function

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

Upvotes: 2

Views: 200

Answers (2)

Shohidur Rahman
Shohidur Rahman

Reputation: 71

You can try it

rtrim(',',$doctors);

Upvotes: 1

Nigel Ren
Nigel Ren

Reputation: 57131

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']);

Upvotes: 1

Related Questions