假设我在“URL”列下有条目,如下所示:
URL
“GET / books / fiction?id = 324223”
“GET /教室/ HTTP / 1.0”
“GET /register.php HTTP / 1.0”
“POST /thankyou.php HTTP / 1.0”
“GET /register.php?error=alreadyregistered HTTP / 1.0”
“POST /processlogin.php?next=%2Fregister.php%3Ferror%3Dalreadyregistered HTTP / 1.0”
“GET /register.php?error=alreadyregistered HTTP / 1.0”
“GET / books / fiction?id = 324273”
我需要一个查询来帮助我从URL中获取第一对斜线中包含的单词“/”。例如。 '教室','注册'等...
条目不限于这些。所以我需要一个通用的查询而不是一个特定的...帮助?
答案 0 :(得分:3)
这应该可以解决问题(如果我理解正确的话):
$request = "GET /books/fiction?id=324223";
// captures URI from request string
$request_array = explode( ' ', $request );
$request_uri = $request_array[ 1 ];
// captures anything between / and . or /
preg_match( '#\/(.*?)[./]#', $request_uri, $matches);
var_dump($matches);
答案 1 :(得分:1)
select substring_index(substring_index('http://books/serious?id=32423','//',-1),'/',1)
答案 2 :(得分:1)
$teststr = array(
"GET /books/fiction?id=324223",
"GET /classroom/ HTTP/1.0",
"GET /register.php HTTP/1.0",
"POST /thankyou.php HTTP/1.0",
"GET /register.php?error=alreadyregistered HTTP/1.0",
"POST /processlogin.php?next=%2Fregister.php%3Ferror%3Dalreadyregistered HTTP/1.0",
"GET /register.php?error=alreadyregistered HTTP/1.0",
"GET /books/fiction?id=324273" );
foreach( $teststr as $str )
if( preg_match( '/\/(?P<folder>\w+)\//', $str, $match) )
echo $match['folder']."<br />";
返回
books
classroom
books
答案 3 :(得分:1)
这个单行应该这样做:
$node = preg_replace('~^.*?/([^./]+).*$~', '$1', $string);
你可以see the output result at Codepad:
books extracted from GET /books/fiction?id=324223
classroom extracted from GET /classroom/ HTTP/1.0
register extracted from GET /register.php HTTP/1.0
thankyou extracted from POST /thankyou.php HTTP/1.0
register extracted from GET /register.php?error=alreadyregistered HTTP/1.0
processlogin extracted from POST /processlogin.php?next=%2Fregister.php%3Ferror%3Dalreadyregistered HTTP/1.0
register extracted from GET /register.php?error=alreadyregistered HTTP/1.0
books extracted from GET /books/fiction?id=324273