1

How can I out from a variable like this: $var1 = "www.game.mysite.com/folder/page.php?var1=1&var2=2";

And then I want somehow to get only the "page.php", what ever it is set to be?

Any ideas how to do this?

4

5 に答える 5

4

I would not use a regex for that, but parse_url and basename:

$var1 = "www.game.mysite.com/folder/page.php?var1=1&var2=2";
$parsed = parse_url($var1);
var_dump(basename($parsed['path']));

See the example on codepad.

于 2012-09-30T01:02:00.733 に答える
2

これは、パスの最後のセグメントを使用します。

$var1 = "www.game.mysite.com/folder/page.php?var1=1&var2=2";
$path = parse_url($var1, PHP_URL_PATH);
$path_parts = explode('/', $path);
var_dump($path_parts[count($path_parts) -1]);

出力:

string(8) "page.php"
于 2012-09-30T01:55:47.567 に答える
0

Some simple regex will get you out of trouble:

$var1 = "www.game.mysite.com/folder/page.php?var1=1&var2=2";
preg_match('/([a-z]+\.php)/', $var1, $file); // Returns `page.php`
于 2012-09-30T00:36:43.453 に答える
0

There's the preg_match method that tehlulz mentioned, or also a preg_replace option in the event you don't know what the file extension is.

$pattern = '/(.*)(\/)(.*)(\?)(.*)/';
$replacement = '$3';
$filename = preg_replace($pattern, $replacement, $var1);

There are several ways to accomplish what you're after.

于 2012-09-30T00:41:09.513 に答える
0

try this

$var1 = "www.game.mysite.com/folder/page.php?var1=fgsd rew1&var2=2";
$reversed_string = strrev($var1);
$pos_of_questionMark = strpos($reversed_string, "?");
$pos_of_first_slash = strpos($reversed_string, "/");
$getfilename = strrev(substr($reversed_string,$pos_of_questionMark+1,  $pos_of_first_slash-$pos_of_questionMark-1));
echo $getfilename;
于 2012-09-30T02:39:17.650 に答える