在 PHP 中使用 HTML 影象標籤

Sarwan Soomro 2024年2月15日
  1. 在 PHP 變數中使用影象標籤
  2. 在 PHP 中儲存影象 URL 源
  3. 在 PHP 中使用 str_replace() 轉換 URL 字串並使用 iframe 標籤
在 PHP 中使用 HTML 影象標籤

在 PHP 中使用 HTML 標記可確保正確使用引號。你只需要將標記儲存在 PHP 變數中。

每當我們用 HTML 標記替換 PHP 變數時,HTML 必須保留在 PHP 字串的引號內。因此,你的 img 標籤的實際引號不會暗示。

單引號:

<?php
$demo = $demo = . '<img src= "\Your Source\" alt="" />';
?>

雙引號:

<?php
$demo2 = $demo2 . "<img src=\Your Image Source\"  />";
?>

現在讓我們舉例說明如何在 PHP 中使用 HTML 標籤。

在 PHP 變數中使用影象標籤

HTML:

<!DOCTYPE html>
<head>
    <title> Use Img Tag inside PHP Script </title>
    <style>
    img {
        height: 400px;
        width: 400px;
        display: block;
        margin-left: auto;
        margin-right: auto
    }
    </style>
</head>
<body align='center'>
    <form action='index.php' method='post'>
        <input type='submit' name='image' value='Use HTML Image Tag inside PHP' />
        <input type='submit' name='video' value='Use HTML Video Tag inside PHP' />
        <input type='submit' name='method2' value='Use HTML Img Tag in PHP method 2' /> </form>
</body>
</html>

PHP 指令碼:

## Note: You do not need to reuse the HTML code above again. It contains markup for all three examples. (Also available in the code directory)

<?php
    if(isset($_POST['image'])){
    $use_img_tag_php = $use_img_tag_php . '<div>
    <img src="demoimage.gif" alt="" /></div>';
    echo $use_img_tag_php;
}
?>

輸出:

在 PHP 變數中使用影象標籤

我們對 PHP 使用了單引號,對 HTML 使用了雙引號。使用此方法,不再需要在影象標籤的源中回顯你的變數。

在 PHP 中儲存影象 URL 源

你只需將 URL 儲存在 PHP 變數中,並在 img 標記的 src 屬性內回顯原始碼。

程式碼:

<?php
if(isset($_POST['method2'])){
//Asign URL path to a variable
    $imgurl = "demoimage.gif"; //Use image URL from your root directory
//Now it dynamic
//Close php tag
?>
<div>
    <img src="<?php
	echo $imgurl ?>" alt="" /></div>

	<?php
}
//close php tag, the isset condition will only load HTML inside its body
?>

輸出:

在 PHP 中儲存影象 URL 源

在 PHP 中使用 str_replace() 轉換 URL 字串並使用 iframe 標籤

如果你複製了任何 youtube URL,並且你想在 HTML iframe 中使用它。同時,你希望它是動態的。

我們將把 HTML 標籤放入 PHP 中,同時使用 str_replace()

程式碼:

<?php
if(isset($_POST['video'])){
$fetchyoutubevideo = "https://www.youtube.com/watch?v=/AkFi90lZmXA";
$embed_it = str_replace("watch?v=", "embed/", $fetchyoutubevideo);
echo $embed_it;
echo $use_video_tag_php = $use_video_tag_php.'<div>
<iframe width="560" height="315"
src="'.$embed_it.'" title="Video Title" frameborder="0" allow="accelerometer;
autoplay; clipboard-write;
encrypted-media; gyroscope; picture-in-picture" allowfullscreen >
</iframe></div>';
}
?>

輸出:

使用 str_replace() 轉換 URL 字串並在 PHP 中使用 iframe 標記

在上面的示例中,我們將 URL 路徑儲存在 $fetchyoutubevideo 中。然後我們使用 str_replace(); 並傳遞三個引數。

  1. 第一個引數 - (watch?v) = 字串
  2. 第二個引數 - (/embed) = 字串
  3. 第三個引數 - ($embed_it) = PHP 變數(URL 字串)

str_replace 函式已將第一個引數替換為第二個引數並將其儲存在第三個引數中。

作者: Sarwan Soomro
Sarwan Soomro avatar Sarwan Soomro avatar

Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.

LinkedIn

相關文章 - PHP HTML