How to check in a regular expression on the existence of a certain attribute and, if it exists, perform manipulations, if not, then do not. You can do this type:

preg_replace("#\[font|font size=(1|2|3)\](.+?)\[\/font\]#is", "...", $text); 

But the size attribute may or may not be transferred. Also, it will be necessary to verify the value of the attribute if it was passed:

 switch($size){ case '1': $f='font-style:10pt;'; case '2': $f='font-style:11pt;'; } 

Tell me how it is possible to implement?

  • one
    hm ... and than preg_match_all did not suit you? - zenith
  • do you understand anything that does [preg_replace] [1] ??? and what is $ size? Where does that come from? Formulate the correct question. [1]: php.net/manual/en/function.preg-replace.php have you ever checked this # [font | font size = (1 | 2 | 3)] (. +?) [\ / Font] # is what is written here if translated into Russian? - Artem
  • $ size is the value of the attribute being passed. In the example I showed what is there now, but it does not work. - rimlin
  • and where is its assignment going in code ??? - Artem
  • This is what I do not know how to implement. - rimlin

1 answer 1

I didn’t test it in combat, but the idea is this:

 preg_match("#\[font|font size=(1|2|3)\](.+?)\[\/font\]#is", $text, $matches); $size=$matches[1]; switch($size){ case '1': $f='font-style:10pt;'; case '2': $f='font-style:11pt;'; }