Misalkan kita memiliki teks seperti berikut :
Nusantara_berdikari : [latex]\begin{aligned} \mathrm{Population_t} &= \alpha_1 + \gamma_1 X_{1,t} + \gamma_2 X_{1,t-1} \\ \mathrm{Price_t} &= \alpha_2 + \beta_1 Z_{1,t} \end{aligned}[/latex] Nusantara_sejahtera
Bagaimana mengganti semua "_" menjadi "\_" kecuali yg berada di dalam tag [latex][/latex] ?
Salah satu solusi dari [1] :
    /**
     * @param string $text 
     * @param array $str_to_replace 
     * @param string $str_replace_with
     */
    public static function replaceTextOutsideTags($text = null, $str_to_replace = [], $str_replace_with = ''){
        $p = implode('|', array_map('preg_quote', $str_to_replace));
        $result = preg_replace("#\[latex\].*?\[\/latex\](*SKIP)(*FAIL)|($p)#i", $str_replace_with, $text);
        return $result ;
    }
Referensi
- preg_replace keywords OUTSIDE of <strong> tags, https://stackoverflow.com/questions/30724791/preg-replace-keywords-outside-of-strong-tags
 
No comments:
Post a Comment