replace a string with double quotes and a pattern using str_replace - str-replace

i want to replace src="images/imagename.jpg" to src="http://xyz.com/images/imagename.jpg"
i tried doing with str_replace. But this does seems to have issue with double quotes. Any easy solutions for this?
thank you

Try using \ escape characters on the double quotes -- like this: \"
return str_replace("src=\"images/imagename.jpg\"", "src=\"http://xyz.com/images/imagename.jpg\"", $string);
With a string passed through htmlentities:
return str_replace("src="images/imagename.jpg"", "src="http://xyz.com/images/imagename.jpg"", $string);

Related

Rails remove '\' from json response

json response
{"skill"=>"{\"dept_id\"=>\"01\", \"user_id\"=>\"001\", \"level_cd\"=>\"04_swim\", \"first_name\"=>\"rohit\", \"last_name\"=>\"patel\", \"dept_full_name\"=>\"swiming\", \"rank\"=>\"04_swim\"}, {\"dept_id\"=>\"02\", \"user_id\"=>\"002\", \"level_cd\"=>\"04_swim\", \"first_name\"=>\"ranjit\", \"last_name\"=>\"shinde\", \"dept_full_name\"=>\"running\", \"rank\"=>\"03_run\"}, {\"dept_id\"=>\"04\", \"user_id\"=>\"004\", \"level_cd\"=>\"02_jump\", \"first_name\"=>\"kedar\", \"last_name\"=>\"patil\", \"dept_full_name\"=>\"jumping\", \"rank\"=>\"02_jump\"}, {\"dept_id\"=>\"05\", \"user_id\"=>\"005\", \"level_cd\"=>\"03_run\", \"first_name\"=>\"kapil\", \"last_name\"=>\"bote\", \"dept_full_name\"=>\"Hammer\", \"rank\"=>\"03_run\"}"
How to remove only \ from this response
expected output is
"skill"=>{"dept_id"=>"01", "user_id"=>"001", "level_cd"=>"04_swim", "first_name"=>"rohit", "last_name"=>"patel", "dept_full_name"=>"swiming", "rank"=>"04_swim"}, {"dept_id"=>"02", "user_id"=>"002", "level_cd"=>"04_swim", "first_name"=>"ranjit", "last_name"=>"shinde", "dept_full_name"=>"running", "rank"=>"03_run"}, {"dept_id"=>"04", "user_id"=>"004", "level_cd"=>"02_jump", "first_name"=>"kedar", "last_name"=>"patil", "dept_full_name"=>"jumping", "rank"=>"02_jump"}, {"dept_id"=>"05", "user_id"=>"005", "level_cd"=>"03_run", "first_name"=>"kapil", "last_name"=>"bote", "dept_full_name"=>"Hammer", "rank"=>"03_run"}
There are currently no backslashes in the string. The backslash is only there because the context is within a double quoted string context.
If you want to use a double quote in double quoted string context you need to escape it with a backslash, otherwise the compiler thinks you want to end the string.
"John Doe said: "Hello Word!""
The above is not valid. The " before Hello World! will end the string. Meaning that Hello World! will not be in string context and Ruby tries to parse Hello and World as constants.
To prevent this from happening you escape the " with a backslash \.
"John Doe said: \"Hello Word!\""
\" will be interpreted as one " character. There is no backslash present within the resulting string. See the Ruby literals documentation.
When using single quotes for string delimiters there is no need to escape the double quotes (but you do need to escape single quotes). The above could also be written as:
'John Doe said: "Hello Word!"'
Similarly your data can be written as:
{"skill"=>'{"dept_id"=>"01", "user_id"=>"001", "level_cd"=>"04_swim", "first_name"=>"rohit", "last_name"=>"patel", "dept_full_name"=>"swiming", "rank"=>"04_swim"}, {"dept_id"=>"02", "user_id"=>"002", "level_cd"=>"04_swim", "first_name"=>"ranjit", "last_name"=>"shinde", "dept_full_name"=>"running", "rank"=>"03_run"}, {"dept_id"=>"04", "user_id"=>"004", "level_cd"=>"02_jump", "first_name"=>"kedar", "last_name"=>"patil", "dept_full_name"=>"jumping", "rank"=>"02_jump"}, {"dept_id"=>"05", "user_id"=>"005", "level_cd"=>"03_run", "first_name"=>"kapil", "last_name"=>"bote", "dept_full_name"=>"Hammer", "rank"=>"03_run"}'
The above clearly demonstrates that there are no backslash characters present in the string.
However the string is not JSON. I suggest changing the server response if possible. You can eval the current response, but I would advise not to use eval ever (eval is evil). If the server would send malicious Ruby code, eval will execute it without any issues and might corrupt your machine.
Looks like the hash example needs to end with an } to be valid. So I added it in my example. Further more it looks to be a collection of records, but it also looks like it's missing a list. If it were inside a list it would be valid but as the example stands now, it is not a valid hash.
But let's say just for fun, I did want to take the string and put it inside an array. Maybe something like this:
data = {"skill"=>"{\"dept_id\"=>\"01\", \"user_id\"=>\"001\", \"level_cd\"=>\"04_swim\", \"first_name\"=>\"rohit\", \"last_name\"=>\"patel\", \"dept_full_name\"=>\"swiming\", \"rank\"=>\"04_swim\"}, {\"dept_id\"=>\"02\", \"user_id\"=>\"002\", \"level_cd\"=>\"04_swim\", \"first_name\"=>\"ranjit\", \"last_name\"=>\"shinde\", \"dept_full_name\"=>\"running\", \"rank\"=>\"03_run\"}, {\"dept_id\"=>\"04\", \"user_id\"=>\"004\", \"level_cd\"=>\"02_jump\", \"first_name\"=>\"kedar\", \"last_name\"=>\"patil\", \"dept_full_name\"=>\"jumping\", \"rank\"=>\"02_jump\"}, {\"dept_id\"=>\"05\", \"user_id\"=>\"005\", \"level_cd\"=>\"03_run\", \"first_name\"=>\"kapil\", \"last_name\"=>\"bote\", \"dept_full_name\"=>\"Hammer\", \"rank\"=>\"03_run\"}"}
parsed_data = data["skill"].split("}, ").map{|x| x.end_with?("\"") ? x + '}' : x}.map{|x| eval(x)}
puts parsed_data
{"dept_id"=>"01", "user_id"=>"001", "level_cd"=>"04_swim", "first_name"=>"rohit", "last_name"=>"patel", "dept_full_name"=>"swiming", "rank"=>"04_swim"}
{"dept_id"=>"02", "user_id"=>"002", "level_cd"=>"04_swim", "first_name"=>"ranjit", "last_name"=>"shinde", "dept_full_name"=>"running", "rank"=>"03_run"}
{"dept_id"=>"04", "user_id"=>"004", "level_cd"=>"02_jump", "first_name"=>"kedar", "last_name"=>"patil", "dept_full_name"=>"jumping", "rank"=>"02_jump"}
{"dept_id"=>"05", "user_id"=>"005", "level_cd"=>"03_run", "first_name"=>"kapil", "last_name"=>"bote", "dept_full_name"=>"Hammer", "rank"=>"03_run"}
Now with the data in an array you can convert it to json if you'd like
require 'json'
2.6.5 :007 > parsed_data.to_json
=> "[{\"dept_id\":\"01\",\"user_id\":\"001\",\"level_cd\":\"04_swim\",\"first_name\":\"rohit\",\"last_name\":\"patel\",\"dept_full_name\":\"swiming\",\"rank\":\"04_swim\"},{\"dept_id\":\"02\",\"user_id\":\"002\",\"level_cd\":\"04_swim\",\"first_name\":\"ranjit\",\"last_name\":\"shinde\",\"dept_full_name\":\"running\",\"rank\":\"03_run\"},{\"dept_id\":\"04\",\"user_id\":\"004\",\"level_cd\":\"02_jump\",\"first_name\":\"kedar\",\"last_name\":\"patil\",\"dept_full_name\":\"jumping\",\"rank\":\"02_jump\"},{\"dept_id\":\"05\",\"user_id\":\"005\",\"level_cd\":\"03_run\",\"first_name\":\"kapil\",\"last_name\":\"bote\",\"dept_full_name\":\"Hammer\",\"rank\":\"03_run\

how to replace " \" " in string in lua

I try to replace the group 2 character \" in my JSON string but not sure why it didnt work.
Here is what i have
raw_json_text_edited = string.gsub( raw_json_text, [[\"]], [[]])
I also use
raw_json_text_edited = string.gsub( raw_json_text, '\"', '')
Both way doesnt work since it somehow only remove the " part of string rather than both \"
A bit background about the problem if you have any other suggestion.
I have a long JSON string with
..."phone":"{\"p1\":\"13068527218\",\"p2\":\"13062225064\",\"p3\":\"14445554444\"}","email":"{\"e1\":\"test#test.com\",\"e2\":\"test2#test.com\",\"e3\":\"sss#ww.com\"}",....
If I remove \" part in this JSON string, everything work.
UPDATE:
WORKING CODE ONLY FOR REPLACING:
raw_json_text_edited = string.gsub( raw_json_text, [[\\"]], [[]])
However, I just discover a problem why my JSON didnt work is that for JSON string,
..."phone":"{\"p1\":\"13068527218\",\"p2\":\"13062225064\",\"p3\":\"14445554444\"}","email":"{\"e1\":\"test#test.com\",\"e2\":\"test2#test.com\",\"e3\":\"sss#ww.com\"}"
If i only replace \" then it would be a sub string in "{}", eg: "{"p1":"1213131"}". This is wrong JSON format since table cannot be string.
This problem is something else so I will put this as solved
raw_json_text_edited = string.gsub( raw_json_text, [[\"]], [[]])

Is there any way to replace double quotes with a backslash in swift

I have a submit form where there are multiple textfields.
Whenever user enters text like "Hi, my name is "xyz"", the service does not accept this JSON due to double quotes in my string.
Please suggest ways to escape this character.
I have tried using encode and decode JSON, replaceOccurrencesOf methods, but none work.
replaceOccurrencesOf()
The below code snippet with replace "(double quote) in a string by \". This will help to replace "(double quote) by any string or character in a given string.
Swift 5 or above
let replacedString = stringToBeModified.replacingOccurrences(of: "\"", with: #"\""#)
Instead of putting the name (i.e., "XYZ" if you getting xyz from textfield ) why not to place (textField.text!) it will not put extra " "

Ruby on Rails String DoubleQuotes

I have a string a = "Hello"
I want to remove double quotes of this String. I am using gsub but its not providing me the correct result.It gives me string with double quotes.
a = "Hello" is how you define a string in Ruby. When using it somewhere else, the double quotes will not be there anymore.
Example:
a = "Hello"
greetings = a + " Prafull"
puts greetings
=> Hello Prafull

How can i get double quoted characters from a string in ruby on rails

If I have a string in a file:
str = hi "Sonal"
I am able to fetch this line of file in a string. Now I want to fetch the characters between the double quotes. i.e. Sonal. How can I do it in ruby?
try the following
'hi "Sonai"'.match(/"(?<inside_quote>.+)"/)[:inside_quote]
You can use regular expression like this,
given_string[/\".*\"/]
This will match the characters under quotes.
or without regexp try something like this s[s.index('"')..s.rindex('"')]

Resources