AnswerBun.com

How to encode base_64 without slash in PHP?

Stack Overflow Asked by Burhan on January 1, 2022

Orignal String:

$string = "Home/Gallery/Images/Mountains";

Then I encode this string into base64_encode():

echo base64_encode($string);

Output:

SG9tZS9HYWxsZXJ5L0ltYWdlcy9Nb3VudGFpbnM=

But I want to show this output:

SG9tZS9H/YWxsZXJ5L0lt/YWdlcy9Nb3V/udGFpbnM=


Look at the Slashes

means I don’t want to that base64_encode() never encode a slash "/" and show slash in orignal form but other string always convert into base64_encode()

There is someone here who can answer me and please also tell how to decode?

(Sorry for bad english)

One Answer

You can split it and use the single parts to encode. You can not exclude any chars.

Note, you will gain some extra padding bytes!

$string = "Home/Gallery/Images/Mountains";

echo join('/', array_map(fn($v) => base64_encode($v), explode('/', $string)));

SG9tZQ==/R2FsbGVyeQ==/SW1hZ2Vz/TW91bnRhaW5z

Answered by Markus Zeller on January 1, 2022

Add your own answers!

Related Questions

Keycloak permission to restrict account based resources

0  Asked on December 26, 2020 by james-lin

 

CSS flex, full height sidebar inside a modal?

1  Asked on December 25, 2020 by ddulla

   

Cant loop through List and display in DataTable

1  Asked on December 25, 2020 by finchy70

 

Automate and looping through batch script

2  Asked on December 25, 2020 by nck_505

       

issue connecting Heroku PHP stack to Redis using Predis

0  Asked on December 25, 2020 by rob-edlin

       

Splitting C linked list without making a copy

2  Asked on December 24, 2020

 

Notice Messages Do Not Appear On The Page

1  Asked on December 24, 2020 by cifhai

 

Making comparisons in a HTTPS GET request in node js

0  Asked on December 24, 2020 by thor_

     

SwiftUI Horizontal ScrollView does not scroll

1  Asked on December 23, 2020 by hillinharwich

     

Calling a tab fragment method inside the parent acitivity

0  Asked on December 23, 2020 by aaron-waller

       

Hiding table rows works, but fading out does not

1  Asked on December 23, 2020 by diamondjoe12

       

How to list kubernetes services in k9s?

1  Asked on December 23, 2020 by matthias-m

 

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP