dantey89 dantey89 - 30 days ago 7
PHP Question

Write regular expression in preg_replace

I still don't understand how regular expression work with preg_replace. I have some url in text:


site.com/user/login.php?valid=tru

site.com/eng/page/some_page.php?valid=tru&anothervar=1


I want to change it so it become this


site.com/user/login/

site.com/eng/page/some_page/


preg_replace(" 'no_mater_what_1'.php'no_mater_what_2' " , 'no_mater_what_1'/ , $some_var);

Answer Source

But why not simply do this:

$replace = explode('.php',$some_var);
$replace = $replace[0] . '/';

Because that I find it necessary to use a regular expression, because ".php" is not repeated in the string.