dantey89
dantey89

Reputation: 2287

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);

Upvotes: 0

Views: 125

Answers (4)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

To avoid traps, like an other .php substring in the path, you can use this replacement:

$url = preg_replace('~\.php(?:[?#]\N*|\z)~i', '', $url, -1, $c);
if (!$c) // not a php file, do something else

or in this way:

if (preg_match('~[^?#]+\.php(?=[?#]|\z)~Ai', $url, $m))
    $url = $m[0];
else
    // not a php file, do something else

This way ensures that the .php matched is the extension of the file because the regex engine will find the leftmost result that is followed by either a ? for the query part, a # for the fragment part or the end of the string.

pattern elements:

  • \N: a character that isn't a newline.
  • \z: anchor for the end of the string.
  • A: modifier that anchors the pattern at the start of the string
  • (?=...): lookahead assertion

The advantage of this approach is the safety with a good efficiency.

An other way with parse_url:

You can use parse_url to separate an url into parts. If this way is a little fastidious because you need to rebuild the url after (and the way you will rebuild it depends of the elements present in the url), it's however far from impossible and provides too a safe way.

Upvotes: 1

kainaw
kainaw

Reputation: 4334

Because your two examples shows up on the same line, this looks a bit confusing. However, it appears that you want to replace everything from .php to the end of the line with a /. So, use:

$new_link = preg_replace('/\.php.*$/', '/', $old_link);

You need the \ in front of the . because . is a special character that needs to be escaped to make it work like a period. Then, you look for php, in that order, followed by anything to the end of the line ($ means end of the line). You replace all of that with /.

Upvotes: 0

Joel Jaime
Joel Jaime

Reputation: 469

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.

Upvotes: 0

jabbink
jabbink

Reputation: 1291

This should work

$subject = 'site.com/eng/page/some_page.php?valid=tru&anothervar=1';

if (preg_match('/(.*)\.php(?:\?.*)/', $subject, $regs)) {
    $result = $regs[1] .'/';
    echo $subject .' => '. $result;
} else {
    echo 'NOT FOUND';
}

The regular expression doing the magic is this

/(.*)\.php(?:\?.*)?/

by parts:

(.*)\.php

Capture everything until (excluding) ".php"

(?:\?.*)

Search for the pattern "?..."

?

Make that last pattern optional

Upvotes: 0

Related Questions