Remove space from query params in url dynamically - ruby-on-rails

I want to remove the space in query params in the request url in ruby
Here is my sample request url:-
URL = 'www.test.com/a?q1=john&q2=US&q3= 92832832&q4=test&q5= foo'
I want to my output as below:-
URL = 'www.test.com/a?q1=john&q2=US&q3=92832832&q4=test&q5=foo'

I suggest trimming the white space. This can be achieved as stated by Joel:
If you want to remove only leading and trailing whitespace (like PHP's trim) you can use .strip, but if you want to remove all whitespace, you can use .gsub(/\s+/, "") instead.
(Ruby function to remove all white spaces?)

To remove whitespace you can use the following on the string
URL = 'www.test.com/a?q1=john&q2=US&q3= 92832832&q4=test&q5= foo'.gsub(/\s+/, "")

url = 'www.test.com/a?q1=john&q2=US&q3= 92832832&q4=test&q5= foo'.gsub!(/\s+/, "")

Related

How do I us the trim function to reduce a URL to just the end page?

I want to take this URL
https://info.example.edu/programs/degree/page-one/
and trim it down to
page-one
by adjusting this formula
=TRIM(RIGHT(SUBSTITUTE(A2,"/",REPT(" ",100)),100))
Here are some things to consider:
avoid using regex
and, the amount of "/"s could change depending on
the location of the page in the URL
Try
=SUBSTITUTE(IF(LEN(A2)-FIND("#",SUBSTITUTE(A2,"/","#",LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))),1)>1,RIGHT(A2,LEN(A2)-FIND("#", SUBSTITUTE(A2,"/","#",LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))))), RIGHT(A2,LEN(A2)-FIND("#",SUBSTITUTE(A2,"/","#",LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))-1)))), "/", "")
This accounts for a forward slash at the end of the URL or not.
Otherwise, if the formatting is always the same,
=SUBSTITUTE(RIGHT(A2,LEN(A2)-FIND("#",SUBSTITUTE(A2,"/","#",LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))-1))), "/", "")
use:
=INDEX(IFNA(REGEXEXTRACT(REGEXEXTRACT(A1:A, "(.+)\/"), "[^/]+$")))
or:
=INDEX(IFNA(REGEXEXTRACT(REGEXEXTRACT(
IF(REGEXMATCH(A1:A, "\/$"), A1:A, A1:A&"/"), "(.+)\/"), "[^/]+$")))

Parse a string with specific condition

How can I trim my string which is in this form:
https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg
to this?
media/93939393hhs8.jpeg
I want to remove all the characters before the second to last slash /.
I can use stringByTrimmingCharactersInSet but I don't know how to specify the condition that I want:
let trimmedString = myString.stringByTrimmingCharactersInSet(
NSCharacterSet.whitespaceAndNewlineCharacterSet() // what here in my case ??
)
The above is for removing the white spaces, but that is not the case here.
Since the string is an URL get the path components, remove anything but the last 2 items and join the items with the slash separator.
if let url = NSURL(string:"https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg"), pathComponents = url.pathComponents {
let trimmedString = pathComponents.suffix(2).joinWithSeparator("/")
print(trimmedString)
}
You're not trimming, you're parsing.
There's no single call that will do what you want. I suggest writing a block of code that uses componentsSeparatedByString("\n") to break it into lines (one URL per line), then parse each line separately.
You could use componentsSeparatedByString("/") on each line to break it into the fragments between your slashes, and then assemble the last 2 fragments together.
(I'm deliberately not writing out the code for you. You should do that for yourself. I'm just pointing you in the right direction.)
You might also be able to use NSURLComponents to treat each line as a URL, but I'm not sure how you'd get the last part of URL before the filename (e.g. "media " or "lego") with that method.

Cleaning a URL with PHP

I've been looking though google and stackflow for an answer for this and testing a few finds but I still can't get this working.
All of these end my link at a space. For example www.website.com/movies/movie
Where I'm trying to get it to read www.website.com/movies/movie with spaces here.mp4
$namehref = "movie/" . $dirArray[$index]. " download";
$DoStream = "Watch";
$DoDownload = "Download";
However this code does not remove the spaces???
$name = $dirArray[$index];
$movienameonly = substr($name, 0, -4);
example www.website.com/movies/movie with spaces here
So my questions are - Why does the first section of code remove the spaces and how do I correct it. In addition to spaces I also hit errors with 's as well.
example They're here.mp4
To remove the spaces completely:
preg_replace("/\s/", "", $your_url);
To replace the spaces with %20 (best way):
preg_replace("/\s/", "%20", $your_url);
To replace spaces with + like url_encode($url) does:
preg_replace("/\s/", "+", $your_url);
To replace ' with %27:
preg_replace("/\s/", "%20", $your_url);
You get errors because spaces can't be inputted in the browser and converts the spaces to %20 and the apostrophe to %27
I found it:
$DoStream = "Watch";
Should have been
$DoStream = "<a href='$the_dir'>Watch</a>";

Generated URL by Html.RouteLink with special characters

I have the following line
#Html.RouteLink(type.Description, "ListingsWithTypeSpecified", new { country = Model.CountryCode, state = Model.State, city = Model.CurrentCity.Name.ToLower(), description = type.Description.ToLower(), id = type.TypeID })
which produces
http://some.com:9609/ca/on/london/physiotherapy%20%20%26%20acupuncture/2
and
http://some.com:9609/ca/on/london/health%20department/19
first one has spaces and a &, second one just has a space
for the first one, I would like to still show
http://localhost:9609/ca/on/london/physiotherapy and acupuncture/2
for this one I understand replacing the & with "and" will work, however I still do not want %20 as spaces instead I would like to have a clean url.
Which method should I be using to properly have friendly url shown by what Html.RouteLink creates?
Replace the space with a -
e.g.
type.Description.ToLower().Replace(" ", "-")

Removing a part of a URL with Ruby

Removing the query string from a URL in Ruby could be done like this:
url.split('?')[0]
Where url is the complete URL including the query string (e.g. url = http://www.domain.extension/folder?schnoo=schnok&foo=bar).
Is there a faster way to do this, i.e. without using split, but rather using Rails?
edit: The goal is to redirect from http://www.domain.extension/folder?schnoo=schnok&foo=bar to http://www.domain.extension/folder.
EDIT: I used:
url = 'http://www.domain.extension/folder?schnoo=schnok&foo=bar'
parsed_url = URI.parse(url)
new_url = parsed_url.scheme+"://"+parsed_url.host+parsed_url.path
Easier to read and harder to screw up if you parse and set fragment & query to nil instead of rebuilding the URL.
parsed = URI::parse("http://www.domain.extension/folder?schnoo=schnok&foo=bar#frag")
parsed.fragment = parsed.query = nil
parsed.to_s
# => "http://www.domain.extension/folder"
url = 'http://www.domain.extension/folder?schnoo=schnok&foo=bar'
u = URI.parse(url)
p = CGI.parse(u.query)
# p is now {"schnoo"=>["schnok"], "foo"=>["bar"]}
Take a look on the : how to get query string from passed url in ruby on rails
You can gain performance using Regex
'http://www.domain.extension/folder?schnoo=schnok&foo=bar'[/[^\?]+/]
#=> "http://www.domain.extension/folder"
Probably no need to split the url. When you visit this link, you are pass two parameters to back-end:
http://www.domain.extension/folder?schnoo=schnok&foo=bar
params[:schnoo]=schnok
params[:foo]=bar
Try to monitor your log and you will see them, then you can use them in controller directly.

Resources