首頁 > 後端開發 > php教程 > PHP Base64_decode 函數

PHP Base64_decode 函數

WBOY
發布: 2024-02-28 08:12:02
轉載
999 人瀏覽過

PHP Base64_decode 函數是PHP程式設計中用於解碼Base64編碼字串的重要函數。透過Base64_decode函數,可以將經過Base64編碼的字串解碼成原始的數據,方便在資料傳輸和預存程序中使用。本文將介紹Base64_decode函數的基本語法和用法,幫助讀者更好地理解並運用該函數。如果您對PHP程式設計和資料處理感興趣,那麼不妨繼續閱讀,以了解更多關於PHP Base64_decode函數的知識。

base64_decode()php 的內建函數,最常用於網路上的郵件功能。

函數語法:

<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span>string base64_decode( <span style="color:#19177c">$string</span>, <span style="color:#19177c">$strict</span>)
</span></span></code></code>
登入後複製

它需要兩個參數。

  1. $string - 用於儲存編碼數據,是必須的。
  2. $strict - 雖然此參數不像前者那樣是強制性的,但當它被設定為TRUE, 時,base64_decode 會回傳FALSE#,前提是它的輸入包含函數字母表以外的資料。否則,無效資料將會自動丟棄。

傳回值:

base64 解碼函數在失敗的情況下傳回 FALSE,並可能傳回二進位值。


PHP Base64_decode 函數範例程式碼1

<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#19177c">$string</span> <span style="color:#666">=</span> base64_encode (<span style="color:#ba2121">'Your string values are encoded'</span>);
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"1.Here is the encoded funct<strong class="keylink">io</strong>n value in a <strong class="keylink">Mac</strong>hine readable f<strong class="keylink">ORM</strong>at = "</span><span style="color:#666">.</span><span style="color:#19177c">$string</span> <span style="color:#666">.</span><span style="color:#ba2121">"<br>"</span>;
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"2.The function decodes the formerly encoded string value"</span> <span style="color:#666">.</span><span style="color:#ba2121">"<br>"</span> <span style="color:#666">.</span>base64_decode(<span style="color:#19177c">$string</span>);
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>
登入後複製

#輸出:

<code><code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>1.Here is the encoded function value in a machine readable 
</span></span><span style="display:flex;"><span>format = WW91ciBzdHJpbmcgdmFsdWVzIGFyZSBlbmNvZGVk
</span></span><span style="display:flex;"><span>2. The function decodes the formerly encoded string value
</span></span><span style="display:flex;"><span>Your string values are encoded.
</span></span></code></code>
登入後複製

範例程式碼顯示了base64_decode() 函數如何在一個簡單的場景中工作。

為了對編碼資料進行解碼,程式為變數賦值,然後與解碼函數一起使用。

而輸出則顯示 base64_decode() 如何將資料轉換回使用者可讀格式。


PHP Base64_decode 函數範例程式碼 2

<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//The following variable is assigned with a string set 
</span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#19177c">$string</span> <span style="color:#666">=</span> <span style="color:#ba2121">"HELLO--こんにちは--你好"</span>; 
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//string cont<strong class="keylink">ai</strong>ns bilingual text 
</span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//base64 is used to encode the data first into the $enco variable
</span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#19177c">$enco</span> <span style="color:#666">=</span> base64_encode (<span style="color:#19177c">$string</span>);
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//finally the base64_decode functionis used to decode the encoded value 
</span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#19177c">$deco</span> <span style="color:#666">=</span> base64_decode (<span style="color:#19177c">$enco</span>); 
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$deco</span>; 
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>
登入後複製

輸出:

<code><code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>HELLO--こんにちは--你好
</span></span></code></code>
登入後複製

以上是PHP Base64_decode 函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:lsjlt.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板