简体   繁体   中英

split a column value based on a delimiter?

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?

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);
select substring_index(substring_index('http://books/serious?id=32423','//',-1),'/',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 />";

returns

books
classroom
books

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM