How to Get Multiple Comma Separated Values from Url

How do I get multiple comma separated values from URL

var url = "http://www.mysite.com/index.html?x=x1&x=x2&x=x3";
var params = url.match(/\?(.*)$/)[1].split('&');
var values = [];
for(var i=0; i<params.length; i++){
values.push( params[i].match(/=(.*)$/)[1] );
}
var result = values.join(","); // "x1,x2,x3"

EDIT: Here is a better solution that lets you select the parameter you want. This is something that I have found buried inside one of my projects, and I didn't write every part of it.

function $_GET(param) {
var query = window.location.search.substring(1);
var vars = query.split('&');
var values = [];
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (urldecode(pair[0]) == param) {
values.push(urldecode(pair[1]));
}
}
return values.join(",");
}

// Decode URL with the '+' character as a space
function urldecode(url) {
return decodeURIComponent(url.replace(/\+/g, ' '));
}

How to pass comma separated multi select values to the url on form submit

I found a solution to this and thought I'd share it in hopes to help others overcome the same problem in the future.

HTML

<select id="region" name="region" multiple>
<option value="1">Region 1</option>
<option value="2">Region 2</option>
<option value="3">Region 3</option>
</select>

<select id="maps" name="maps[]" multiple>
<option value="1">Map 1</option>
<option value="2">Map 2</option>
<option value="3">Map 3</option>
</select>

<button id="search">Search</button>

JavaScript

<script>
$(function() {
$("#search").click(function() {
var params = {};
var getSelect = ['region', 'maps'];

$.each(getSelect, function(index, value) {
var select = $('#' + value);

if (select.val() != '') {
var selected = select.val();

if (select.attr('multiple'))
selected = selected.join(',');

params[value] = selected;
}
});

if (!$.isEmptyObject(params)) {
var url = [location.protocol, '//', location.host, location.pathname].join('');

window.location.href = url + '?' + $.param(params);
}
});
});
</script>

How to extract comma separated values from query parameter in Go?

There may not be such a way.
Usually I do this:

type QueryParams struct {
Type string `form:"type"`
}

func (q *QueryParams) Types() []string {
return strings.Split(q.Type, ",")
}

func BulkRead(c *gin.Context) {
params := new(QueryParams)

if err := c.ShouldBindQuery(¶ms); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "couldn't bind query params"})
return
}
c.JSONP(200, map[string]interface{}{
"types": params.Types(),
})
}

How do I split a query value which has commas?

$vals = explode(',', $_GET['q']);

Creating Rest url with comma separated and multiple parameters

You should be doing something like below.

HttpUrl.Builder urlBuilder = HttpUrl.parse("https://roads.googleapis.com/v1/nearestRoads").newBuilder();
urlBuilder.addQueryParameter("parameters", "value1,value2|value3,value4");
urlBuilder.addQueryParameter("key", "YOUR_API_KEY");
String url = urlBuilder.build().toString();
Request request = new Request.Builder().url(url).build();

Whether you should be doing loop or something else for value1,value2 etc, depends on, how you are fetching this values in your application, since question is less clear on that, its hard to answer that part. Regarding | to %2c, I think you should be doing it.

How to get comma separated JSON data on its on line?

If you have control of the API design, something like

{
title: 'Company Name',
markets: [
{ id: 4, name: 'Asia' },
{ id: 4, name: 'US' }
]
}

comma delimited string as an array of urls

  1. Try to use a set of characters that you will never find in a url like $&$ to separate your values, that will act as a comma, so when you retreive the values you will do this: $arr = explode('$&$', $string);

  2. You can also build an array and use json_encode and save the string, you will get rid of the explode function, example:

    $arrayOfUrls = [your array];
    $urlsInJson = json_encode($arrayOfUrls);

    Then store $urlsInJson in database

    To retrieve your data you will use

    $arrayOfUrls = json_decode($yourQueryResult['URLS'])

Hope it helps.



Related Topics



Leave a reply



Submit