Reputation: 391
Suppose I have an entries under a column 'URL' like so:
URL
"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"
I need a query that will help me get just the words enclosed in the first pair of slashes ,'/' from the URL. Eg. 'classroom','register' etc...
And the entries arent limited to these.. So i'd need a generic query rather than a specific one..Help?
Upvotes: 2
Views: 305
Reputation: 154513
This one-liner should do it:
$node = preg_replace('~^.*?/([^./]+).*$~', '$1', $string);
You can 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
Upvotes: 1
Reputation: 12356
$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 />";
returns
books
classroom
books
Upvotes: 1
Reputation: 31910
This should do the trick (if I understand the question correctly):
$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);
Upvotes: 3
Reputation: 56357
select substring_index(substring_index('http://books/serious?id=32423','//',-1),'/',1)
Upvotes: 1