简体   繁体   中英

Php url handling -urlencode not working -Stirng to url -special charater handling

String  = "KOA Axial Carbon Film Resistor 3.3k? ±5% 0.25W -450 ? +350ppm/°C"

Error link after encode

http://www.api.simplyduty.com/api/Classification/get-hscode?APIKey=A36AFAA1-A570-4279-84CC-ECE4B9EB76F6&fullDescription=KOA+Axial+Carbon+Film+Resistor+3.3k%CE%A9+%C2%B15%25+0.25W+-450+%E2%86%92+%2B350ppm%2F%C2%B0C&originCountry=AM&destinationCountry=AW

but i want this type of conversion as browser do conversion

http://www.api.simplyduty.com/api/Classification/get-hscode?APIKey=A36AFAA1-A570-4279-84CC-ECE4B9EB76F6&fullDescription=KOA%20Axial%20Carbon%20Film%20Resistor%203.3k?%20%C2%B15%%200.25W%20-450%20?%20+350ppm/%C2%B0C&originCountry=AM&destinationCountry=AW

correct string how can i get this

"KOA%20Axial%20Carbon%20Film%20Resistor%203.3k?%20%C2%B15%%200.25W%20-450%20?%20+350ppm/%C2%B0C"

You can take a look at http://php.net/rawurlencode

php -r "echo rawurlencode('KOA Axial Carbon Film Resistor 3.3k? ±5% 0.25W -450 ? +350ppm/°C');"
KOA%20Axial%20Carbon%20Film%20Resistor%203.3k%3F%20%C2%B15%25%200.25W%20-450%20%3F%20%2B350ppm%2F%C2%B0C

Warning, your expected "correct" string forgets to encode 4 chars : / % ? +

the slash is encoded by "%2F", the "%" is encoded by "%25", the "?" is encoded by "%3F", the "+" is encoded by "%2B"

So you can obtain your expected string like this:

php -r "echo str_replace(['%2F', '%25', '%3F', '%2B'], ['/', '%', '?', '+'],  rawurlencode('KOA Axial Carbon Film Resistor 3.3k? ±5% 0.25W -450 ? +350ppm/°C'));"
KOA%20Axial%20Carbon%20Film%20Resistor%203.3k?%20%C2%B15%%200.25W%20-450%20?%20+350ppm/%C2%B0C

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM