Changing ereg_replace to equivalent preg_replace

Possible Duplicates:
How can I convert ereg expressions to preg in PHP?

What is the equivalent using preg_replace to the following expression?

ereg_replace('^' . $g_lang_prefix, '', $t_var );

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

preg_replace('/^' . preg_quote($g_lang_prefix,'/') . '/', '', $t_var );

If you need $g_lang_prefix working as a common regex then omit preg_quote

preg_replace('/^' . $g_lang_prefix . '/', '', $t_var );

(quite obvious)

Also if you need this second solution, but your $g_lang can contain even this char / then you need to escape at least it:

preg_replace('/^' . str_replace('/','\/',$g_lang_prefix) . '/', '', $t_var );

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply