How To Analyze Url To Auto Add Appropriate Encoding On Appropriate Spots ? - TechRepublic
Question
June 26, 2021 at 02:50 AM
rahulsharmaa11199

How To Analyze Url To Auto Add Appropriate Encoding On Appropriate Spots ?

by rahulsharmaa11199 . Updated 4 years, 11 months ago

Hey There,

My question is I need to parse user submitted urls before echoing their submitted urls on my page. Need to parse the urls as I won’t know what urls they will be submitting nor the structures of their urls.
On each submitted url, I need to add the key part of each query string on the $key array and the value part of each query string on the $value array. How to do this ?
Example, a user submits this url:
http://example.com/autos/cars/list.php?state=california&max_price=50000

FIRST STEP
In this example, I need the $key array to be populated with:
state
max_price

And, I need the $value array to be populated with:
california
50000

Then I need to echo each array’s values.
How to do this ?

SECOND STEP
I need to auto add the appropriate encoding functions in the submitted url. So now I won’t be echoing like this:

echo ‘http://example.com/autos/cars/list.php?state=california&max_price=50000’;

But after parsing the url and after adding the appropriate encoding php functions in the correct spots of the url, I will be echoing like this:

echo rawurlencode(‘http://example.com/autos/cars/list.php’) .’?state=’ .urlencode(‘california’) .’&max_price=’ .intval(50000);

Did you notice that, I auto added rawurlencode on the file path and urlencode on the query string’s values and did you notice that I added intval where the query string’s value was an INT ?
Now, I did all this additions manually here ofcourse to show you what I want php to do on auto-pilot.

SUMMARY:
Need to get php to auto analyze the url and auto add the rawurlencode(), urlencode() and intval() where appropriate.

How to do this ?

Once these two steps are achieved, then I can say a custom php function just got built that analyzes submitted urls and encodes the urls with the appropriate encoding functions (rawurlencode, urlencode, intval, etc).

I don’t know how to start on this and so any code sample would be most appreciated.
This bad code didn’t work. Failed attempt:

print_r(parse_url($url)); echo ‘
‘;

$keys = array(print_r(parse_url($url))); echo ‘
‘;

foreach($keys as $key=>$value)
{
$key = array();
$value = array();
echo $key[‘0’]; echo ‘
‘;
echo $value[‘0’]; echo ‘
‘;
}

All Comments