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?
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?
これは、パスの最後のセグメントを使用します。
$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"
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`
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.
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;