PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, May 17, 2022

[FIXED] How to remove space from image name and replace with % in PHP?

 May 17, 2022     php     No comments   

Issue

I want to remove the space between image name and replace it with % . How can I achieve that?

Image path = 'http://combined/nature image GREY_120_240_Glossy_OBL Premium.jpg'.

I want it to be = 'http://combined/nature%20image%20GREY_120_240_Glossy_OBL%20Premium.jpg'

Code I have tried

$url = 'http://combined/nature image GREY_120_240_Glossy_OBL Premium.jpg';
            $decodeUrl = urlencode ($url);

            $name = basename($url);
            $upload = file_put_contents("uploads/$name",file_get_contents($url));
            if($upload){
                echo "okk";

            }

Solution

You need to use rawurlencode and do it after pulling the name, presuming you want it on the naming.

$url = 'http://combined/nature image GREY_120_240_Glossy_OBL Premium.jpg';
$decodeUrl = urlencode($url);
$name = basename($url);
echo rawurlencode($name);

https://3v4l.org/CptPc

I would replace any whitespace with underscores.

$url = 'http://combined/nature image GREY_120_240_Glossy_OBL Premium.jpg';
$name = preg_replace('/\s+/', '_', basename($url));


Answered By - user3783243
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing