简体   繁体   中英

Cleaning up URL in PHP

I'd like to "clean up" my URL using PHP ZF2 framework. The URL is like following:

http://sitename.com/testingtesting/?inviter123

The part in between site name and the query string parameter is the one that I'd like to take out which is:

testingtesting

Also the name doesn't have to be necessarily testingtesting, it can be test, or any other name, the length of the string can vary, which is what confuses me mostly. The question is here, how to take out everything in between the / / lines regardless of its size...

PS What I've done so far is the following:

 $controllerName =  substr($_SERVER["REQUEST_URI"],1,10);

Which returns me the following:

/testingtesting/?inviter123

Now the issue is how to get rid of the unwanted content in the string...

Edit:

I've figured to use the substr method in PHP, I skip the first element in the string array, but how to get the dynamic length of whats in between /hereitcanbeanysize/...

Tried Robert's solution, this is what I get:

array(4) { ["scheme"]=> string(4) "http" ["host"]=> string(12) "sitename.com" ["path"]=> string(16) "/testingtesting/" ["query"]=> string(10) "inviter123" }

Now I need to get rid of the // in the "path" element of the array... How can I do that ?

You can use parse_url() function and merge host, scheme and path without query part.

Check this code:

$urlParts = parse_url('http://sitename.com/testingtesting/?inviter123');

echo str_replace('/' , '', $urlParts['path']);

I think the best way to do what you want is rewritting the url in: .htaccess -if you use Apache server-, or web.config -if you use IIS-.

In adition you may capture the string name before your querystring to use it in your app logic.

.htaccess example:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/testingtesting/(.*) /$1 [QSA]

If you just want to display the clean url:

$cleanUrl = str_replace('/testingtesting','', $_SERVER["REQUEST_URI"]);

If you are using ZF2 you should not use native php functions for these kind of things, you should use the RouteMach class to find/retrieve segments of your matched url:

'route_name' => array(
    'type' => 'segment',
    'options' => array(
        'route' => '/:controller[/:action]',
        'constraints' => array(
            'controller' => '[a-zA-Z][a-zA-Z0-9_-]+',
            'action'     => '[a-zA-Z][a-zA-Z0-9_-]+',
        ),
        'defaults' => array(
            'controller' => 'Application\Controller\IndexController',
            'action'     => 'index',
        ),
    )
)

Now you can get the controller and action from the route-match ( Zend\\Mvc\\Router\\RouteMatch );

/** @var MvcEvent $event */
$routeMatch = $event->getRouteMatch();
$controller = $routeMatch->getParam('controller');
$action = $routeMatch->getParam('action');

Check also the ZF2 documentation on Routing here .

You are using a framework, make sure you know how to use it properly. Using native php methods like substr and parse_url is definitely NOT the way to go when you want to get the segments from your url.

Check a tutorial to get familiar with the ZF2 basics, for example the ZF2 skeleton application (also known as the album application)

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