There is a line $this->lists['categories'] , which contains a select tag, which in turn contains a list of categories. So this select has the following form already on the page <select id="catid" name="catid"> <option value="0" selected="selected">- Выберите категорию -</option></select>

Well, then a list of categories, so I need to set the class this select . I try through preg_replace() as follows:

 <?php $arr=preg_replace("<select>", "select class='selectpicker' data-live-search='true'", $this->lists['categories']) ?> <?php echo $arr; ?> 

It seems to replace, but also replaces the selected attribute (i.e., cuts it to select and ed ). Tell me how to avoid this? Maybe there is a regular expression for such a case. Thank.

    2 answers 2

    Signs denoting a tag (around select) are used as search line delimiters. Other characters are commonly used, such as "/". Those. should be like this:

     preg_replace("/<select>/", ...) 

    But then this code will not work if there is something inside the select tag (id or something else). You also need to search only inside the Select tag, I would suggest doing something like this:

     preg_replace("/<select([^>]*)>/", "<select class='newclass' $1>", ...); 

      You can "head on" with the usual replace like this:

       str_replace('select id=', 'select class="selectpicker" data-live-search="true" id=', $this->lists['categories']) 
      • The author needs to parse the house and collect according to the mind, and not replace an unknown number of selections with abum - Naumov