Home > Backend Development > PHP Tutorial > PHP determines the character type. PHP inserts new characters at the specified position in the string.

PHP determines the character type. PHP inserts new characters at the specified position in the string.

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-28 08:28:55
Original
1106 people have browsed it

 Because the project uses the DataTable table to load background data and needs to connect the table to query the policy status selected by the virtual machine, so I thought of taking out the content of the policy table first to form a '' string, spell its policy value into a string like 'value="1"' when traversing the virtual machine list, and then use explode() and the implode() function to form a new string and return it to the front desk to achieve the selected state.

<span><span> 1    </span><span>$option</span> = '<select>'<span>;
</span><span> 2    </span><span> 3    </span><span>//</span><span> 取出所有策略</span><span> 4    </span><span>$sla_query</span> = <span>$this</span>->db->select('sla_id, name')->get('sla'<span>);
</span><span> 5</span><span>$sla_res</span> = <span>$sla_query</span>-><span>result_array();
</span><span> 6</span><span>if</span> (!<span>empty</span>(<span>$sla_res</span><span>)) {
</span><span> 7</span><span>$option</span> .= '<option value="0">未保护</option>'<span>;
</span><span> 8</span><span>foreach</span> (<span>$sla_res</span><span>as</span><span>$k1</span> => <span>$v1</span><span>) {
</span><span> 9</span><span>$option</span> .= '<option value="' . <span>$v1</span>['sla_id'] . '">' . <span>$v1</span>['name'] . '</option>'<span>;
</span><span>10</span><span>            }
</span><span>11</span><span>$option</span> .= '</select>'<span>;
</span><span>12</span><span>13</span>         } <span>else</span><span> {
</span><span>14</span><span>$option</span> .= '<option value="0">未保护</option></select>'<span>;
</span><span>15</span><span>        }
</span><span>16</span><span>17</span><span>18    </span><span>//</span><span> 选中默认的保护策略</span><span>19    </span><span>$vm_query</span> = <span>$this</span>->db->select('sla_id')->where('vm_id', <span>$</span>vm_id)->get('task_vm', 1<span>);
</span><span>20</span><span>$vm_res</span> = <span>$vm_query</span>-><span>row_array();
</span><span>21    </span><span>if</span> (<span>is_null</span>(<span>$vm_res</span>['sla_id'])) <span>$res</span>['sla_id'] = 0<span>;
</span><span>22    </span><span>$selected</span> = 'value="' . <span>$vm_res</span>['sla_id'] . '"'<span>;
</span><span>23    </span><span>$new_str_arr</span> = <span>explode</span>(<span>$selected</span>, <span>$option</span><span>);
</span><span>24</span><span>$new_option</span> = <span>implode</span>(" {<span>$selected</span>} selected", <span>$new_str_arr</span>);</span>
Copy after login

The above introduces PHP to determine the character type. PHP inserts new characters at the specified position in the string, including the content of PHP to determine the character type. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template