Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5986543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:41:11+00:00 2026-05-22T22:41:11+00:00

I have a text input where users submit CSV e.g. red, blue, red, yellow

  • 0

I have a text input where users submit CSV e.g. red, blue, red, yellow

If the user submits duplicate values for instance red as seen above I want to remove the duplicate. I started making a callback but I’m not sure how to complete it.

    //callback rule
    function _remove_dublicate($str) 
    {
            $val = strtolower($str); //make everything lowercase
            $colors = str_getcsv($val); //create array                          
            $result = array_unique($colors); //remove duplicates

    }

If there are duplicates, what should I do to submit the new string from $resultto the database?

Below is my form validation

$this->form_validation->set_rules('colors', 'Colors', 'trim|required|xss_clean|regex_match[/^[a-z, ]+$/i]|callback__remove_dublicate');


        if ($this->form_validation->run() == FALSE) //if validation rules fail
        {           
            $this->load->view('edit/form');
        }
        else //success
        {
            $data = array (
                'colors' => $this->input->post('colors')
            );          
            $this->My_model->colors_update($data);          
        }

EDIT

based on Cabaret’s suggestion I added this in the else statement for removing dublicates

$colors = str_getcsv($this->input->post('colors')); //create array
$result = array_unique($colors); //remove duplicates
$comma_separated = implode(",", $result); //add back CSV string

$data = array (
    'colors' => $comma_separated
);          

It seems to work

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:41:12+00:00Added an answer on May 22, 2026 at 10:41 pm

    Despite the comments, this is a perfectly valid reason to use a callback, similar to the “prep” rules that actually change the value of the input submitted (for instance: trim, xss_clean, strtolower).

    You’re on the right track, all you have to do is return $result in your callback and it will alter the input, but make sure you return a string. Example:

    //callback rule
    function _remove_duplicate($str = '') 
    {
        $val = strtolower($str); //make everything lowercase
        $colors = str_getcsv($val); //create array 
        // you could also use explode(',', $colors)
        // if you aren't expecting other delimiters than a comma
        // i.e. no commas within quotes                         
        $result = array_unique($colors); //remove duplicates
        $result = implode(',', $result); // back to string value
        return $result; // Return the value to alter the input
    }
    

    Now, if you wanted to warn the user that there are duplicates instead of simply removing them, just return FALSE if any are found, maybe with count($colors) === count($result) while $result is still an array. It’s up to you, but just so you know the option to return true/false or alter the input is available.

    You could actually write something like this into a new form validation rule within the Form_validation class itself (extend the library, or even just a global function) and not need to use callbacks, it seems like it would be a very reusable function.

    This is considered a “prep” rule. From the User Guide:

    In addition to the validation functions like the ones we used above, you can also prep your data in various ways. For example, you can set up rules like this:

    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
    $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
    

    In the above example, we are “trimming” the fields, converting the password to MD5, and running the username through the “xss_clean” function, which removes malicious data.

    Any native PHP function that accepts one parameter can be used as a rule, like htmlspecialchars, trim, MD5, etc.

    Note: You will generally want to use the prepping functions after the validation rules so if there is an error, the original data will be shown in the form.

    So, after you’ve checked that the input is valid, you can proceed to clean it, trim, remove duplicates from a comma separated string, or anything you want. Any globally available functions are valid (including non-native PHP functions, for example: any of CI’s helper functions, as long as they are loaded and defined.), as well as any functions that belong to the Form_validation library or any extension of it you may have.

    One thing to be aware of: If the function does not exist or is not available, it will be silently ignored. Always test to make sure you are getting the correct results.

    Hope this clears up some confusion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a simple text input field users put in a number. I need
I have a textarea input on a form for users to submit their snail
I have a text box. I want to take user input from there and
I have a text box in an HTML form where the user will input
I have one text input and one button (see below). How can I use
I have this input text: <html><head><meta http-equiv=content-type content=text/html; charset=utf-8></head><body><table cellspacing=0 cellpadding=0 border=0 align=center width=603>
I need to be able to take an arbitrary text input that may have
I have an input field where both regular text and sprintf tags can be
I have saved input from a textarea element to a TEXT column in MySQL.
I have this input in a form: <input type=text value=<script src='/js/script.js' type='text/javascript'></script> name=embed/> The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.