修剪表单字段数据

Trimming Form Field Data

我的网页上有一个 html 表单域供用户提交 YouTube 'share' 视频 links。

不过,我只希望link的一部分通过。

使用 YouTube 'copy and paste''share' 选项时,他们的 URL 似乎都以:

开头

https://youtu.be/

我想做的(如果可能的话)是当我的用户输入完整的 URL...

例如: https://youtu.be/TP8RB7UZHKI

我只希望:TP8RB7UZHKI 出现在 php 结果页面上。

我希望 URL 的 https://youtu.be/ 部分始终被省略(从开头删除)。

我可以指示我的网站访问者在填写表格时这样做,但这可能会让他们中的大多数人感到困惑,而且我不能犯错误。

我在下面包含了一个非常精简版的 html 表单和 php 结果页面代码。

再次...我不知道这是否可能,但如果可以,我将不胜感激 and/or 如何实现这一目标的示例。

表单页面:

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    body {margin-top:100px; margin-left:50px;}
    .videoURL{width:300px; height:25px;}
    </style>
    </head>

    <body>
    <form action="videoURLUploadResults.php" method="post" enctype="multipart/form-data">

    Video URL
    <input class="videoURL" ID="videoURL" name="videoURL" value="" autocomplete="off"/>

    <input class="submitButton" type="submit" name="submit" value="SUBMIT">
    </button>

    </form>
    </body>
    </html>

视频URL上传结果PHP页面:

    <!DOCTYPE html>
    <html>
    <head>
    <style>body{margin-top:100px; margin-left:50px;}</style>
    </head>

    <body>

    <?php $videoURL = ($_POST['videoURL']); echo  $videoURL;?>

    </body>
    </html>
//on This page Use string replace function 
<!DOCTYPE html>
<html>
<head>
<style>body{margin-top:100px; margin-left:50px;}</style>
</head>

<body>

<?php $videoURL = ($_POST['videoURL']);    

 $videoURLFinal=str_replace('https://youtu.be/', '', $videoURL);

 echo $videoURLFinal;
?>

</body>
</html>

您可以在 php 中使用 str_ireplace()https://youtu.be/ 替换为空字符串。它的语法:

str_ireplace(find,replace,string,count)

您的 php 代码将是:

 <?php $videoURL = ($_POST['videoURL']); echo  str_ireplace("https://youtu.be/","",$videoURL);?>

如果“https://youtu.be/”在用户输入的每个 link 中都很常见,您可以尝试 php 的 substr(string,start,length) 函数。

示例:-

$link="https://youtu.be/TP8RB7UZHKI"; //your post data from your example i.e $_POST['videoURL']

$startlen=strlen("https://youtu.be/");// length of the string you want to remove

$totallen=strlen($link); //total length of the string

$videoURL=substr($link,$startlen,$totallen); // using substring to get the result

echo $videoURL;