Php Gurus,
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:
**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 ‘
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(‘
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:
[code]
print_r(parse_url($url)); echo ‘<br>’;
$keys = array(print_r(parse_url($url))); echo ‘<br>’;
foreach($keys as $key=>$value)
{
$key = array();
$value = array();
echo $key[‘0’]; echo ‘<br>’;
echo $value[‘0’]; echo ‘<br>’;
}
>@developer_web#1633291 echo rawurlencode('[http://example.com/autos/cars/list.php](http://example.com/autos/cars/list.php) ')
<i>
</i>echo rawurlencode('http://example.com/autos/cars/list.php');
// http%3A%2F%2Fexample.com%2Fautos%2Fcars%2Flist.php
>@developer_web#1633291 Need to get php to auto analyze the url and auto add the rawurlencode(), urlencode() and intval() where appropriate.
<i>
</i>$url = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
echo prepare_url($url) . "n";
function prepare_url($url) {
$url_parts = parse_url($url);
if($url_parts === false or empty($url_parts['host'])) {
return false;
}
$url_out = preg_match('/^https?/i', $url_parts['scheme']) ? strtolower($url_parts['scheme']) : 'https';
$url_out .= "://{$url_parts['host']}{$url_parts['path']}";
if(!empty($url_parts['query'])) {
parse_str($url_parts['query'], $query_parts);
foreach($query_parts as $q_key => $q_value) {
$query_string_parts[] = urlencode($q_key).'='.urlencode($q_value);
}
$url_out .= '?'.implode('&', $query_string_parts);
}
return $url_out;
}
<i>
</i>foreach($query_string_parts as $query_string_part)
{
echo $query_string_part; echo '<br>';
}
foreach($q_key as $q_keyy)
{
echo $q_keyy; echo '<br>';
}
foreach($q_value as $q_valuee)
{
echo $q_valuee; echo '<br>';
}
foreach($query_string_parts as $query_string_part)
{
echo $query_string_part; echo '<br>';
}
<i>
</i><?php
$url = 'http://nogdog.com/autos/cars/list.php?state=california&max_price=50000';
echo prepare_url($url) . "n";
function prepare_url($url) {
$url_parts = parse_url($url);
if($url_parts === false or empty($url_parts['host'])) {
return false;
}
$url_out = preg_match('/^https?/i', $url_parts['scheme']) ? strtolower($url_parts['scheme']) : 'https';
$url_out .= "://{$url_parts['host']}{$url_parts['path']}";
if(!empty($url_parts['query'])) {
parse_str($url_parts['query'], $query_parts);
foreach($query_parts as $q_key => $q_value) {
$query_string_parts[] = urlencode($q_key).'='.urlencode($q_value);
}
$url_out .= '?'.implode('&', $query_string_parts);
}
return $url_out;
}
foreach($query_string_parts as $query_string_part)
{
echo $query_string_part; echo '<br>';
}
foreach($q_key as $q_keyy)
{
echo $q_keyy; echo '<br>';
}
foreach($q_value as $q_valuee)
{
echo $q_valuee; echo '<br>';
}
?>
<i>
</i><?php
$url = 'http://nogdog.com/autos/cars/list.php?state=california&max_price=50000';
echo prepare_url($url) . "n";
function prepare_url($url) {
$url_parts = parse_url($url);
if($url_parts === false or empty($url_parts['host'])) {
return false;
}
$url_out = preg_match('/^https?/i', $url_parts['scheme']) ? strtolower($url_parts['scheme']) : 'https';
$url_out .= "://{$url_parts['host']}{$url_parts['path']}";
if(!empty($url_parts['query'])) {
parse_str($url_parts['query'], $query_parts);
foreach($query_parts as $q_key => $q_value) {
$query_string_parts[] = urlencode($q_key).'='.urlencode($q_value);
}
$url_out .= '?'.implode('&', $query_string_parts);
}
<i> </i>foreach($query_string_parts as $query_string_part)
<i> </i>{
<i> </i> echo $query_string_part; echo '<br>';
<i> </i>}
<i> </i>foreach($q_key as $q_keyy)
<i> </i>{
<i> </i> echo $q_keyy; echo '<br>';
<i> </i>}
<i> </i>foreach($q_value as $q_valuee)
<i> </i>{
<i> </i> echo $q_valuee; echo '<br>';
<i> </i>}
<i> </i>foreach($query_string_parts as $query_string_part)
<i> </i>{
<i> </i> echo $query_string_part; echo '<br>';
<i> </i>}
//return $url_out;
}
<i>
</i><?php
$url = 'http://example.com/cat/subcat?var1=value1&var2=value2';
var_dump(parse_url($url));
$scheme = parse_url($url, PHP_URL_SCHEME);
$user = parse_url($url, PHP_URL_USER);
$pass = parse_url($url, PHP_URL_PASS);
$host = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$path = parse_url($url, PHP_URL_PATH);
$query_strings = parse_url($url, PHP_URL_QUERY);
$fragment = parse_url($url, PHP_URL_FRAGMENT);
echo $parsed_url = $scheme .'://' .$user .$pass .$host .$port .$path .'?' .$query_strings .$fragment;
echo '<br>';
$query_strings_array = array();
$query_strings_array[] = implode("&", $query_strings);
foreach ($query_strings_array as $key=>$value)
{
<i> </i>$query_strings_keys[] = $key;
<i> </i>$query_strings_values[] = $value;
<i> </i>
<i> </i>echo $query_strings_keys[-1];
<i> </i>echo $query_strings_values[-1];
}
?>
<i>
</i>$url = 'http://example.com/cat/subcat?var1=value1&var2=value2';
// parse url get query
$query_strings = parse_url($url, PHP_URL_QUERY);
$query_strings_array = [];
// parse query into array
parse_str($query_strings, $query_strings_array);
// separate keys
$query_strings_keys = array_keys($query_strings_array);
// separate values
$query_strings_values = array_values($query_strings_array);
echo $url . "rnrn";
// loop and echo
for($i=0; $i<count($query_strings_array); $i++){
$num = $i+1;
$k = $query_strings_keys[$i];
$v = $query_strings_values[$i];
echo "#{$num} Key: {$k} Value: {$v} rn";
}
// keys array
print_r($query_strings_keys);
// values array
print_r($query_strings_values);
<i>
</i><?php
$url = 'http://example.com/cat/subcat?var1=value1&var2=value2';
// Get query string from the URL
$query_string = parse_url($url, PHP_URL_QUERY);
// Split the query string into individual parameters and return it in $arr
parse_str($query_string, $arr);
// Store keys i.e. parameter names in $keys
$keys = array_keys($arr);
// Store values i.e. parameter values in $values
$values = array_values($arr);
// print
print_r($keys); echo '<br>';
print_r($values); echo '<br>';
?>
<i>
</i>$url = 'http://example.com/cat/subcat?var1=value1&var2=value2';
$scheme = parse_url($url, PHP_URL_SCHEME);
$user = parse_url($url, PHP_URL_USER);
$pass = parse_url($url, PHP_URL_PASS);
$host = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$path = parse_url($url, PHP_URL_PATH);
//$query_strings = parse_url($url, PHP_URL_QUERY);
$fragment = parse_url($url, PHP_URL_FRAGMENT);
//echo $parsed_url = $scheme .'://' .$user .$pass .$host .$port .$path .'?' .$query_strings .$fragment;
echo '<br>'; echo '<br>';
// Get query string from the URL
$query_strings = parse_url($url, PHP_URL_QUERY);
// Split the query string into individual parameters and return it in $arr
parse_str($query_strings, $arr);
// Store keys i.e. parameter names in $keys
$keys = array_keys($arr);
// Store values i.e. parameter values in $values
$values = array_values($arr);
// print
print_r($arr); echo '<br>';
print_r($keys); echo '<br>';
print_r($values); echo '<br>';
?>
<i>
</i>echo $url . "rnrn";
<i>
</i>echo $url;
echo '<br>'; //Added this line.
<i>
</i>echo '<br>'; //Added this line.
[code]
Zorg Code Amended
[code]
$url = 'http://example.com/cat/subcat?var1=value1&var2=value2';
// parse url get query
$query_strings = parse_url($url, PHP_URL_QUERY);
$query_strings_array = [];
// parse query into array
parse_str($query_strings, $query_strings_array);
// separate keys
$query_strings_keys = array_keys($query_strings_array);
// separate values
$query_strings_values = array_values($query_strings_array);
echo $url . "rnrn";
echo '<br>'; //Added this line.
// loop and echo
for($i=0; $i<count($query_strings_array); $i++){
$num = $i+1;
$k = $query_strings_keys[$i];
$v = $query_strings_values[$i];
echo "#{$num} Key: {$k} Value: {$v} rn";
}
echo '<br>'; //Added this line.
// keys array
print_r($query_strings_keys);
echo '<br>'; //Added this line.
// values array
print_r($query_strings_values);
echo '<br>'; //Added this line.
?>
"rnrn"
<i>
</i>function encodedUrl($url){
$query_strings_array = [];
$query_string_parts = [];
// parse URL & get query
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
$query_strings = parse_url($url, PHP_URL_QUERY);
<i> </i> // parse query into array
<i> </i> parse_str($query_strings, $query_strings_array);
<i> </i>
<i> </i> // separate keys & values
<i> </i> $query_strings_keys = array_keys($query_strings_array);
<i> </i> $query_strings_values = array_values($query_strings_array);
<i> </i>
<i> </i> // loop query
<i> </i> for($i = 0; $i < count($query_strings_array); $i++){
<i> </i> $k = urlencode($query_strings_keys[$i]);
<i> </i> $v = $query_strings_values[$i];
<i> </i> $val = is_numeric($v) ? intval($v) : urlencode($v);
<i> </i>
<i> </i> $query_string_parts[] = "{$k}={$val}";
<i> </i> }
<i> </i>
<i> </i> // re-assemble URL
<i> </i> $encodedHostPath = rawurlencode("{$scheme}://{$host}{$path}");
<i> </i>
<i> </i> return $encodedHostPath . '?' . implode('&', $query_string_parts);
}
$url1 = 'http://example.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=13';
$url2 = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
// run urls thru function & echo
echo encodedUrl($url1);
echo '<br/>';
echo encodedUrl($url2);
echo '<br/>';
<i>
</i>function prepare_url($url) {
$url_parts = parse_url($url);
if($url_parts === false or empty($url_parts['host'])) {
return false;
}
// re-assemble the start of url string
$url_start = preg_match('/^https?/i', $url_parts['scheme']) ? strtolower($url_parts['scheme']) : 'https';
$url_start .= "://{$url_parts['host']}{$url_parts['path']}";
// rawurlencode the start of url string
$url_out = rawurlencode($url_start);
if(!empty($url_parts['query'])) {
parse_str($url_parts['query'], $query_parts);
foreach($query_parts as $q_key => $q_value) {
// assemble and check if value is numeric
$query_string_parts[] = urlencode($q_key).'='.(is_numeric($q_value) ? intval($q_value) :urlencode($q_value));
}
$url_out .= '?'.implode('&', $query_string_parts);
}
return $url_out;
}
$url = 'http://example.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=13';
echo prepare_url($url);
<i>
</i>$url1 = 'http://example.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=13';
$url2 = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
// run urls thru function & echo
echo encodedUrl($url1);
echo '<br/>';
echo encodedUrl($url2);
echo '<br/>';
<i>
</i>$url1 = 'http://example.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=13';
$url2 = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
// run urls thru function & echo
$encoded_url1 = encodedUrl($url1);
$encoded_url2 = encodedUrl($url2);
echo $link1 = '<a href=' ."$encoded_url1" .'>' ."$encoded_url1" .'</a>';
echo '<br/>';
echo $link2 = '<a href=' ."$$encoded_url2" .'>' ."$encoded_url2". '</a>';
echo '<br/>';
<i>
</i>function encodedUrl($url){
$query_strings_array = [];
$query_string_parts = [];
// parse URL & get query
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
$query_strings = parse_url($url, PHP_URL_QUERY);
<i> </i>// parse query into array
<i> </i>parse_str($query_strings, $query_strings_array);
<i> </i>// separate keys & values
$query_strings_keys = array_keys($query_strings_array);
$query_strings_values = array_values($query_strings_array);
// loop query
for($i = 0; $i < count($query_strings_array); $i++){
$k = urlencode($query_strings_keys[$i]);
$v = $query_strings_values[$i];
$val = is_numeric($v) ? intval($v) : urlencode($v);
<br/>
<i> </i> $query_string_parts[] = "{$k}={$val}";
}
// re-assemble URL
$encodedHostPath = rawurlencode("{$scheme}://{$host}{$path}");
return $encodedHostPath . '?' . implode('&', $query_string_parts);
}
$url1 = 'http://example.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=13';
$url2 = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
// run urls thru function & echo
// run urls thru function & echo
$encoded_url1 = encodedUrl($url1);
$encoded_url2 = encodedUrl($url2);
echo $link1 = '<a href=' ."$encoded_url1" .'>' ."$encoded_url1" .'</a>';
echo '<br/>';
echo $link2 = '<a hre
<i>
</i>// http://example.com/cat/subcat is the URL you're going to be navigating to
// urlencode('http://example.com/autos/cars/list.php?state=california&max_price=50000') is this:
http%3A%2F%2Fexample.com%2Fautos%2Fcars%2Flist.php%3Fstate%3Dcalifornia%26max_price%3D50000
// pass the URL string to the subcat page you would link it like this
http://example.com/cat/subcat?url=http%3A%2F%2Fexample.com%2Fautos%2Fcars%2Flist.php%3Fstate%3Dcalifornia%26max_price%3D50000
// HTML of the link would be:
<a href="http://example.com/cat/subcat?url=http%3A%2F%2Fexample.com%2Fautos%2Fcars%2Flist.php%3Fstate%3Dcalifornia%26max_price%3D50000" target="_blank">
// on subcat page $_GET['url'] would be:
$_GET['url'] = 'http%3A%2F%2Fexample.com%2Fautos%2Fcars%2Flist.php%3Fstate%3Dcalifornia%26max_price%3D50000'
urlencode()
<i>
</i>$url1 = 'http://example.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=13';
$url2 = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
// run urls thru function & echo
echo encodedUrl($url1);
echo '<br/>';
echo encodedUrl($url2);
echo '<br/>';
<i>
</i><?php
function encodedUrledited($url){
$query_strings_array = [];
$query_string_parts = [];
// parse URL & get query
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
$query_strings = parse_url($url, PHP_URL_QUERY);
<i> </i>// parse query into array
<i> </i>parse_str($query_strings, $query_strings_array);
<i> </i>// separate keys & values
$query_strings_keys = array_keys($query_strings_array);
$query_strings_values = array_values($query_strings_array);
// loop query
for($i = 0; $i < count($query_strings_array); $i++){
$k = urlencode($query_strings_keys[$i]);
$v = $query_strings_values[$i];
$val = is_numeric($v) ? intval($v) : urlencode($v);
<br/>
<i> </i> $query_string_parts[] = "{$k}={$val}";
}
// re-assemble URL
$encodedHostPath = rawurlencode("{$scheme}://{$host}{$path}");
//return $encodedHostPath . '?' .implode('&', $query_string_parts);
return $encodedHostPath . '?' .htmlentities(implode('&', $query_string_parts));
}
if(!ISSET($_POST['url1']) && empty($_POST['url1']) && !ISSET($_POST['url2']) && empty($_POST['url2']))
{
//Default Values for Substituting empty User Inputs.
$url1 = 'http://example.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=13';
$url2 = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
}
else
{
//user has made following inputs...
$encoded_url1 = encodedUrledited($_POST['url1']);
$encoded_url2 = encodedUrledited($_POST['url2']);
}
echo $link1 = '<a href=' .$encoded_url1 .'>' .$encoded_url1 .'</a>';
echo '<br/>';
echo $link2 = '<a href=' .$encoded_url2 .'>' .$encoded_url2. '</a>';
echo '<br>';
?>
<i>
</i><form method = 'POST'>
<label for='find'>Name</label>
<input type='text' name='name' id='name'>
<br>
<label for='url'>Url</label>
<input type='text' name='url' id='url'>
<br>
<label for='link_'description'>Link Description</label>
<input type='text' name='link_description' id='link_description'>
<button type='submit'>Submit!</button>
</form>
<i>
</i>echo $serp_url = rawurlencode(https://localhost/Templates/url_encode_Template.php) . '?search=' .urlencode(keyword) .'&tbl=' .'urlencode(links) .'&col=' .urlencode(keyword) .'&max=100' .'&page=' .intval(1)
<i>
</i><form method = 'POST'>
<label for='find'>Name</label>
<input type='text' name='name' id='name'>
<br>
<label for='url'>Url</label>
<input type='text' name='url' id='url'>
<br>
<label for='link_'description'>Link Description</label>
<input type='text' name='link_description' id='link_description'>
<button type='submit'>Submit!</button>
</form>
<?php
$user_submitted_name = $_POST[name];
$user_submitted_url = $_POST[url];
$user_submitted_link_description = $_POST[link_description];
echo htmlspecialchars($user_submitted_name);
echo htmlspecialchars($user_submitted_url);
echo htmlspecialchars($user_submitted_link_description);
?>
<i>
</i><form method = 'POST'>
<label for='find'>Name</label>
<input type='text' name='name' id='name'>
<br>
<label for='url'>Url</label>
<input type='text' name='url' id='url'>
<br>
<label for='link_'description'>Link Description</label>
<input type='text' name='link_description' id='link_description'>
<button type='submit'>Submit!</button>
</form>
<?php
$user_submitted_name = $_POST[name];
$user_submitted_url = $_POST[url];
$user_submitted_link_description = $_POST[link_description];
echo $user_submitted_name;
echo $user_submitted_url;
echo $user_submitted_link_description;
?>
<i>
</i><form method = 'POST'>
<label for='find'>Name</label>
<input type='text' name='name' id='name'>
<br>
<label for='url'>Url</label>
<input type='text' name='url' id='url'>
<br>
<label for='link_'description'>Link Description</label>
<input type='text' name='link_description' id='link_description'>
<button type='submit'>Submit!</button>
</form>
<?php
$user_submitted_name = $_POST[name];
$user_submitted_url = $_POST[url]; //Url is: daveyerwin.com/autos/keywords=cars&model=bmw&price=50000&page=10
$user_submitted_link_description = $_POST[link_description];
echo htmlspecialchars($user_submitted_name);
echo '<br>';
$url_secured = rawurlencode(http://daveyerwin.com/autos/) .'?keywords=' .urlencode(cars) .'&model=' .urlencode(bmw) .'&price=' .intval(50000) .'&page=' .intval(10);
echo '<br>';
echo '<a href=' .htmlspecialchars($url_secured) .'>' .htmlspecialchars($url_secured) .'</a>' ;
echo '<br>';
echo htmlspecialchars($user_submitted_link_description);
?>
<i>
</i><?php
//NogDog's Code
//https://www.webdeveloper.com/d/394997-how-to-analyze-url-to-auto-add-appropriate-encoding-on-appropriate-spots/2
$url = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
echo prepare_url($url) . "n";
function prepare_url($url) {
$url_parts = parse_url($url);
if($url_parts === false or empty($url_parts['host'])) {
return false;
}
$url_out = preg_match('/^https?/i', $url_parts['scheme']) ? strtolower($url_parts['scheme']) : 'https';
$url_out .= "://{$url_parts['host']}{$url_parts['path']}";
if(!empty($url_parts['query'])) {
parse_str($url_parts['query'], $query_parts);
foreach($query_parts as $q_key => $q_value) {
$query_string_parts[] = urlencode($q_key).'='.urlencode($q_value);
}
$url_out .= '?'.implode('&', $query_string_parts);
}
return $url_out;
}
?>
<i>
</i><?php
//Zorg's 2nd code (updating NogDog's by substituting the regex). To auto add urlencode(), rawurlencode() & intval().
//https://www.webdeveloper.com/d/394997-how-to-analyze-url-to-auto-add-appropriate-encoding-on-appropriate-spots/10
function encodedUrl($url){
$query_strings_array = [];
$query_string_parts = [];
// parse URL & get query
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
$query_strings = parse_url($url, PHP_URL_QUERY);
<i> </i>// parse query into array
<i> </i>parse_str($query_strings, $query_strings_array);
<i> </i>// separate keys & values
$query_strings_keys = array_keys($query_strings_array);
$query_strings_values = array_values($query_strings_array);
// loop query
for($i = 0; $i < count($query_strings_array); $i++){
$k = urlencode($query_strings_keys[$i]);
$v = $query_strings_values[$i];
$val = is_numeric($v) ? intval($v) : urlencode($v);
<br/>
<i> </i> $query_string_parts[] = "{$k}={$val}";
}
// re-assemble URL
$encodedHostPath = rawurlencode("{$scheme}://{$host}{$path}");
return $encodedHostPath . '?' . implode('&', $query_string_parts);
}
$url1 = 'http://example.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=13';
$url2 = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
// run urls thru function & echo
// run urls thru function & echo
echo $encoded_url1 = encodedUrl($url1); echo '<br>';
echo $encoded_url2 = encodedUrl($url2); echo '<br>';
?>
<i>
</i>$url = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
echo prepare_url($url) . "n";
function prepare_url($url) {
$url_parts = parse_url($url);
if($url_parts === false or empty($url_parts['host'])) {
return false;
}
$url_out = preg_match('/^https?/i', $url_parts['scheme']) ? strtolower($url_parts['scheme']) : 'https';
$url_out .= "://{$url_parts['host']}{$url_parts['path']}";
if(!empty($url_parts['query'])) {
parse_str($url_parts['query'], $query_parts);
foreach($query_parts as $q_key => $q_value) {
$query_string_parts[] = urlencode($q_key).'='.urlencode($q_value);
}
$url_out .= '?'.implode('&', $query_string_parts);
}
return $url_out;
}
<i>
</i>$url_out = preg_match('/^https?/i', $url_parts['scheme']) ? strtolower($url_parts['scheme']) : 'https';
<i>
</i><?php
//NogDog's Code
//https://www.webdeveloper.com/d/394997-how-to-analyze-url-to-auto-add-appropriate-encoding-on-appropriate-spots/2
$url = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
echo prepare_url($url) . "n";
function prepare_url($url) {
$url_parts = parse_url($url);
if($url_parts === false or empty($url_parts['host'])) {
return false;
}
$url_out = preg_match('/^https?/i', $url_parts['scheme']) ? strtolower($url_parts['scheme']) : 'https';
$url_out .= "://{$url_parts['host']}{$url_parts['path']}";
if(!empty($url_parts['query'])) {
parse_str($url_parts['query'], $query_parts);
foreach($query_parts as $q_key => $q_value) {
$query_string_parts[] = urlencode($q_key).'='.urlencode($q_value);
}
$url_out .= '?'.implode('&', $query_string_parts);
}
return $url_out;
}
?>
<i>
</i><?php
//Zorg's 2nd code (updating NogDog's by substituting the regex). To auto add urlencode(), rawurlencode() & intval().
//https://www.webdeveloper.com/d/394997-how-to-analyze-url-to-auto-add-appropriate-encoding-on-appropriate-spots/10
function encodedUrl($url){
$query_strings_array = [];
$query_string_parts = [];
// parse URL & get query
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
$query_strings = parse_url($url, PHP_URL_QUERY);
<i> </i>// parse query into array
<i> </i>parse_str($query_strings, $query_strings_array);
<i> </i>// separate keys & values
$query_strings_keys = array_keys($query_strings_array);
$query_strings_values = array_values($query_strings_array);
// loop query
for($i = 0; $i < count($query_strings_array); $i++){
$k = urlencode($query_strings_keys[$i]);
$v = $query_strings_values[$i];
$val = is_numeric($v) ? intval($v) : urlencode($v);
<br/>
<i> </i> $query_string_parts[] = "{$k}={$val}";
}
// re-assemble URL
$encodedHostPath = rawurlencode("{$scheme}://{$host}{$path}");
return $encodedHostPath . '?' . implode('&', $query_string_parts);
}
$url1 = 'http://example.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=13';
$url2 = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
// run urls thru function & echo
// run urls thru function & echo
echo $encoded_url1 = encodedUrl($url1); echo '<br>';
echo $encoded_url2 = encodedUrl($url2); echo '<br>';
?>
<i>
</i>$url = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
echo prepare_url($url) . "n";
function prepare_url($url) {
$url_parts = parse_url($url);
if($url_parts === false or empty($url_parts['host'])) {
return false;
}
$url_out = preg_match('/^https?/i', $url_parts['scheme']) ? strtolower($url_parts['scheme']) : 'https';
$url_out .= "://{$url_parts['host']}{$url_parts['path']}";
if(!empty($url_parts['query'])) {
parse_str($url_parts['query'], $query_parts);
foreach($query_parts as $q_key => $q_value) {
$query_string_parts[] = urlencode($q_key).'='.urlencode($q_value);
}
$url_out .= '?'.implode('&', $query_string_parts);
}
return $url_out;
}
<i>
</i>$url_out = preg_match('/^https?/i', $url_parts['scheme']) ? strtolower($url_parts['scheme']) : 'https';
<i>
</i>$url = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
echo prepare_url($url) . "n";
function prepare_url($url) {
$url_parts = parse_url($url);
if($url_parts === false or empty($url_parts['host'])) {
return false;
}
$url_out = preg_match('/^https?/i', $url_parts['scheme']) ? strtolower($url_parts['scheme']) : 'https';
$url_out .= "://{$url_parts['host']}{$url_parts['path']}";
if(!empty($url_parts['query'])) {
parse_str($url_parts['query'], $query_parts);
foreach($query_parts as $q_key => $q_value) {
$query_string_parts[] = urlencode($q_key).'='.urlencode($q_value);
}
$url_out .= '?'.implode('&', $query_string_parts);
}
return $url_out;
}
<i>
</i>$url_out = preg_match('/^https?/i', $url_parts['scheme']) ? strtolower($url_parts['scheme']) : 'https';
<i>
</i><?php
//NogDog's Code
//https://www.webdeveloper.com/d/394997-how-to-analyze-url-to-auto-add-appropriate-encoding-on-appropriate-spots/2
$url = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
echo prepare_url($url) . "n";
function prepare_url($url) {
$url_parts = parse_url($url);
if($url_parts === false or empty($url_parts['host'])) {
return false;
}
$url_out = preg_match('/^https?/i', $url_parts['scheme']) ? strtolower($url_parts['scheme']) : 'https';
$url_out .= "://{$url_parts['host']}{$url_parts['path']}";
if(!empty($url_parts['query'])) {
parse_str($url_parts['query'], $query_parts);
foreach($query_parts as $q_key => $q_value) {
$query_string_parts[] = urlencode($q_key).'='.urlencode($q_value);
}
$url_out .= '?'.implode('&', $query_string_parts);
}
return $url_out;
}
?>
<i>
</i><?php
//Zorg's 2nd code (updating NogDog's by substituting the regex). To auto add urlencode(), rawurlencode() & intval().
//https://www.webdeveloper.com/d/394997-how-to-analyze-url-to-auto-add-appropriate-encoding-on-appropriate-spots/10
function encodedUrl($url){
$query_strings_array = [];
$query_string_parts = [];
// parse URL & get query
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
$query_strings = parse_url($url, PHP_URL_QUERY);
// parse query into array
parse_str($query_strings, $query_strings_array);
// separate keys & values
$query_strings_keys = array_keys($query_strings_array);
$query_strings_values = array_values($query_strings_array);
// loop query
for($i = 0; $i < count($query_strings_array); $i++){
$k = urlencode($query_strings_keys[$i]);
$v = $query_strings_values[$i];
$val = is_numeric($v) ? intval($v) : urlencode($v);
<br/>
$query_string_parts[] = "{$k}={$val}";
}
// re-assemble URL
$encodedHostPath = rawurlencode("{$scheme}://{$host}{$path}");
return $encodedHostPath . '?' . implode('&', $query_string_parts);
}
$url1 = 'http://example.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=13';
$url2 = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
// run urls thru function & echo
// run urls thru function & echo
echo $encoded_url1 = encodedUrl($url1); echo '<br>';
echo $encoded_url2 = encodedUrl($url2); echo '<br>';
?>
>@developer_web#1635248 And, can you add intval() and rawurlencode() too where you added the urlencode() ?
<?php
//Zorg's 2nd code (updating NogDog's by substituting the regex). To auto add urlencode(), rawurlencode() & intval().
//https://www.webdeveloper.com/d/394997-how-to-analyze-url-to-auto-add-appropriate-encoding-on-appropriate-spots/10
function encodedUrl($url){
$query_strings_array = [];
$query_string_parts = [];
// parse URL & get query
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
$query_strings = parse_url($url, PHP_URL_QUERY);
// parse query into array
parse_str($query_strings, $query_strings_array);
// separate keys & values
$query_strings_keys = array_keys($query_strings_array);
$query_strings_values = array_values($query_strings_array);
// loop query
for($i = 0; $i < count($query_strings_array); $i++){
$k = urlencode($query_strings_keys[$i]);
$v = $query_strings_values[$i];
$val = is_numeric($v) ? intval($v) : urlencode($v);
$query_string_parts[] = "{$k}={$val}";
}
// re-assemble URL
$encodedHostPath = rawurlencode("{$scheme}://{$host}{$path}");
return $encodedHostPath . '?' . implode('&', $query_string_parts);
}
$url1 = 'http://example.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=13';
$url2 = 'http://example.com/autos/cars/list.php?state=california&max_price=50000';
// run urls thru function & echo
// run urls thru function & echo
echo $encoded_url1 = encodedUrl($url1); echo '<br>';
echo $encoded_url2 = encodedUrl($url2); echo '<br>';
?>
<i>
</i>$encodedHostPath = rawurlencode("{$scheme}://{$host}{$path}");
<i>
</i>$encodedHostPath = rawurlencode("{$scheme}").'://'.rawurlencode("{$host}").$path;
0.1.9 — BETA 5.19