Php Regex - how to choose if something

class Something

{

    public static function compile(&$subject, $replace, $with) {

        $placeholders = array_combine($replace, $with);



        $condition  = '{[a-z0-9\_\- ]+:[a-z_]+}';

        $inner      = '((?:(?!{/?if).)*?)';     

        $pattern    = '#{if\s?('.$condition.')}'.$inner.'{/if}#is';



        while (preg_match($pattern, $subject, $match)) {

            $placeholder = $match[1];           

            $content     = $match[2];



    // if empty value remove whole line

    // else show line but remove pseudo-code

    $subject = preg_replace($pattern,

                            empty($placeholders[$placeholder]) ? '' : addcslashes($content, '$'),

                            $subject,

                            1);

        }

    }

}

I have an area of ​​html code to play. Dompdf processes the rest to convert my form to pdf. This code was almost prepared. It gives me the freedom to use;

 {if {dil:value}} <div class="english">ENGLISH</div> {/if} 

something similar in the html area. But he checked only empty or not. But I want to know which option is selected. So I want to use a code like this:

 {if {dil:value}=='ENGLISH'} <div class="english">ENGLISH</div> {/if}  

I converted an empty control string with a manual equality check to make sure it works;

        // if empty value remove whole line

        // else show line but remove pseudo-code

        $subject = preg_replace($pattern,

                                $placeholders[$placeholder]=='english' ? '' : addcslashes($content, '$'),

                                $subject,

                                1);

And it worked, but without freedom of course (only if the radio is equal to English). I am so new to regex, so I couldn’t understand. I tried to add the $ equality variable, so I would use my code many times with different checks;

class Something

{

    public static function compile(&$subject, $replace, $with) {

        $placeholders = array_combine($replace, $with);



        $condition  = '{[a-z0-9\_\- ]+:[a-z_]+}';

        $inner      = '((?:(?!{/?if).)*?)';

        $equality   = '(?<=~)[^}]*(?=~)';

        $pattern    = '#{if\s?('.$condition.')'.$equality.'}'.$inner.'{/if}#is';



        while (preg_match($pattern, $subject, $match)) {

            $placeholder = $match[1];           

            $content     = $match[2];



            // if empty value remove whole line

            // else show line but remove pseudo-code

            $subject = preg_replace($pattern,

                                    $placeholders[$placeholder]==$equality ? '' : addcslashes($content, '$'),

                                    $subject,

                                    1);

        }

    }

}

with this code in the html area;

{if {dil:value}~'ENGLISH'~} <div class="english">ENGLISH</div> {/if} 

, , : PDF, pdf.

- $match. $match , . , ,

   while (preg_match($pattern, $subject, $match)) {

        $placeholder = $match[1];  

        $equality   =  $match[2];     

        $content    = $match[3];

, - . , , .

.

+4
1

:

class Something
{

    public static function compile(&$subject, $replace, $with) {

        $placeholders = array_combine($replace, $with);



        $condition  = '{[a-z0-9\_\- ]+:[a-z_]+}';

        $inner      = '((?:(?!{\/?if).)*?)';

        $equality   = '(~[A-ZÇŞĞÜÖİçşğüöı]+~)';

        $pattern    = '#{if\s?('.$condition.')}'.$equality.''.$inner.'{\/if}#is';



        while (preg_match($pattern, $subject, $match)) {

            $placeholder = $match[1];           

            $equality    = $match[2];

            $content     = $match[3];

            $equality = substr($equality, 1, -1);

            // if empty value remove whole line

            // else show line but remove pseudo-code

            $subject = preg_replace($pattern,

                                    $placeholders[$placeholder] == $equality ? '' : addcslashes($content, '$'),

                                    $subject,

                                    1);

        }

    }

}

Utf-8 . . , , html, :

{if {language:value}}~FRENCH~<div class="english">ENGLISH</div>{/if}

, , - , - , , . , , :) , , , . 2 .

, :)

0

Source: https://habr.com/ru/post/1649511/


All Articles