Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php alternative to explode

<?php
$URI_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
echo 'http://' . $_SERVER['HTTP_HOST'] . $URI_parts[0];
?>

I've read that explode was rather heavy on resources, and I was curious if there's an optimal alternative that will perform the same task in this case.

The code above is destroying everything following a query string ? including parameters. I need to find the least costly method to do this.

like image 806
questy Avatar asked Aug 30 '26 22:08

questy


2 Answers

You should use parse_url(), like this :

$URI_parts = parse_url( $_SERVER['REQUEST_URI'] );
echo 'http://' . $_SERVER['SERVER_NAME'] . $URI_parts['path'];
like image 154
cmorrissey Avatar answered Sep 01 '26 16:09

cmorrissey


You could use strstr($_SERVER['REQUEST_URI'], "?", TRUE). Alternatively, you could use strtok($_SERVER['REQUEST_URI'], "?").

It should stop matching after it's satisfied.

As an aside, you shouldn't not use a PHP function because you heard [citation needed] that it was slow. You should use the appropriate tool for the job, and if that's too slow after finding a performance issue, measuring it and determining it's your use of that function, then consider refactoring.

like image 36
alex Avatar answered Sep 01 '26 16:09

alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!