Php inserts new characters at the specified position of the string.
Php inserts new characters at the specified position of a string
Because the project uses a DataTable table to load background data and needs to connect the table to query the selected policy status of the VM, it is expected to retrieve the policy table content first, to form a '<select> <option value = "1"> </option> [n options] </select>' string, when traversing the Virtual Machine list, the policy value is combined into a string like 'value = "1" 'and then reused.Explode ()AndImplode ()Function to form a new string and return it to the foreground.
$ Option = '<select class = "sla_list">'; // retrieve all policies $ sla_query = $ this-> db-> select ('sla _ id, name ') -> get ('sla '); $ sla_res = $ sla_query-> result_array (); if (! Empty ($ sla_res) {$ option. = '<option value = "0"> unprotected </option>'; foreach ($ sla_res as $ k1 => $ v1) {$ option. = '<option value = "'. $ v1 ['sla _ id']. '"> '. $ v1 ['name']. '</option>';} $ option. = '</select>';} else {$ option. = '<option value = "0"> unprotected </option> </select> ';} // select the default protection policy $ vm_query = $ this-> db-> select ('sla _ id')-> where ('vm _ id', $ vm_id) -> get ('Task _ vm ', 1); $ vm_res = $ vm_query-> row_array (); if (is_null ($ vm_res ['sla _ id']) $ res ['sla _ id'] = 0; $ selected = 'value = "'. $ vm_res ['sla _ id']. '"'; $ new_str_arr = explode ($ selected, $ option); $ new_option = implode (" {$ selected} selected ", $ new_str_arr );
The above is a simple implementation of php to insert new characters in the specified position of the string for everyone. I hope you can support more help ~