Add %20 between terms returned from database - php

I have a database of 2 word names and they have a space between them however in order for my api call to work I need a %20 between the first and second word.
Currently mysql_query returns Anas platyrhynchos and I need it to return Anas%20platyrhynchos
any quick solution?

You can use PHP urlencode. That should solve your problem.

you can use the replace() function to return the correct string from your query
replace(columnname,' ','%20')
Alternatively you can use the urlencode() function to do this in your PHP code

You can do this on the PHP side using urlencode

You will need to urlencode the returned result from the database. This will add %20 and other special URL characters I am assuming you need.

Use Replace function.
Select Replace(colName, ' ', '%20')

Related

How to parse a string using regex or another technique?

I need to parse the id from the following string:
https://itunes.apple.com/us/album/24k-magic/id1161503945?i=1161504024&uo=2
I need to only return the following:
id1161503945
The string always begins with https://itunes.apple.com/ and ends with ?i=#####&uo=2
I tried string and replace with wildcards but that did not work.
Well, you can use this below regex. It is working. I have use preg_replace function.
$data = 'https://itunes.apple.com/us/album/24k-magic/id1161503945?i=1161504024&uo=2';
echo preg_replace("/(.*)\/(\w+)\?(.*)/","$2",$data);
Output is
id1161503945
Or You can use
preg_match("/(\/)(\w+)(\?)/",$data,$m);
echo $m[2];
Same output.
Hope it help you
If it's really always the last element (before query params) in the url, then you can use this simple regex:
'/id[^?]+/'
CAUTION: as pointed by #xhienne, this works only if you're sure that another id string doesn't appear anywhere before the searched part.
If it may happen, rather use:
'/id[\d]+/'
This way, it's safe with respect to a previous id string, but the searched id must be followed by digits only.

How to get "+" form an URL using GET method in PHP?

From the below URL
http://nis.com/projects/cc_intranet/mauth/lib_test/?authenticate=7JaoTs9NM4xdTnZpQE+q73X4N0oqMvX+BSlLrsqDeUL6VwaXt/91ZYOviomSIt/DvPuEjKAvip5++++++++++UZuVQWJ53mQa83Dz5EX4sfbjI1iXQjHrdwa2Ecca1bLe6MHis9UuSs
When i use "$_GET['authenticate'];" and then print it,
i get the the following output where "+" doesn't display.
7JaoTs9NM4xdTnZpQE q73X4N0oqMvX
BSlLrsqDeUL6VwaXt/91ZYOviomSIt/DvPuEjKAvip5
UZuVQWJ53mQa83Dz5EX4sfbjI1iXQjHrdwa2Ecca1bLe6MHis9UuSs
Is there any way how to get the exact same result ?? (i.e) in my case the "+" symbol has not been passed to the $_GET['authenticate'] while printing it ???
+ is reserved character in GET queries to represent a space character. If you really need + character in your query you need to replace it with %2B:
authenticate=7JaoTs9NM4xdTnZpQE%2Bq73X4N0oqMvX%2BBSlLrsqDeUL6VwaXt/91ZYOviomSIt/DvPuEjKAvip5%2B%2B%2B%2B%2B%2B%2B%2B%2B%2BUZuVQWJ53mQa83Dz5EX4sfbjI1iXQjHrdwa2Ecca1bLe6MHis9UuSs
urlencode php function is a good way to create a proper URL with all reserved symbols escaped.
urldecode function is for decoding such URLs.

PHP replace special characers

I am trying to get IP details from this page http://www.geoplugin.net/json.gp?ip= in PHP.
But sometimes it returns some characters like this š. Is there any way to replace it with the respective character (š - š)?
Try function html_entity_decode()

str_replace on a posted form

i want to replace any " ' " that people post on my form with nothing as it's causing errors down the line at the moment i've tried this
$visit = str_replace("","'",$this->input->post('visit_type'));
This Changes nothing, is their an easier way around this?
Bare in mind im just a humble apprentice developer. Don't get too technical.
You could use htmlspecialchars and addslashes that convert special characters to HTML entities and quote string with slashes but if you want to use str_replace, the correct usage is $visit = str_replace("'","",$this->input->post('visit_type'));
The first paramater is the search value and the second the replacement value that replaces found search values
str_replace("'","",$this->input->post('visit_type'));
please read the documentation on str_replace..
http://uk1.php.net/str_replace
Swap your parameters around:
$visit = str_replace("'","",$this->input->post('visit_type'));

How to use URLs that contain ampersands (&) with phpTumb?

Let's say I have the following URL: pic.php?t=1&p=23
When I try phpThumb.php?src=/pic.php?t=1&p=23, I get Forbidden parameter: p.
Anyone knows if there's a way around it?
You need to URLEncode that ampersand if you are trying to pass p=23 to pic.php. Try replacing the & with %26.
What you are effectively doing there is passing p=23 as a parameter to phpThumb.php, which obviously it doesn't like...
What you probably want to do is 'phpThumb.php?src='.urlencode('/pic.php?t=1&p=23');
Read this and this.
urlencode() the URL before passing it to phpThumb.
You can URL encode the ampersand to make it part of the content of the parameter in the querystring. This way, the whole /pic.php?t=1&p=23 gets passed as the src.
phpThumb.php?src=/pic.php?t=1%26p=23
The %26 is the encoded version of the &.
Use urlencode().

Categories