【问题标题】:Trimming Form Field Data修剪表单字段数据
【发布时间】:2017-06-15 08:01:01
【问题描述】:

我的网页上有一个 html 表单字段供用户提交 YouTube“分享”视频链接。

但是,我只希望通过链接的一部分。

当使用 YouTube 的“复制和粘贴”“分享”选项时,他们的 URL 似乎都以:

https://youtu.be/

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

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

我只希望:TP8RB7UZHKI 显示在 php 结果页面上。

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

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

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

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

表单页面:

    <!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>

【问题讨论】:

    标签: javascript php forms youtube trim


    【解决方案1】:
    //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>
    

    【讨论】:

    • 感谢您的帮助。
    【解决方案2】:

    如果“https://youtu.be/”在用户输入的每个链接中都是常见的,你可以试试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;
    

    【讨论】:

    • 感谢您的帮助。
    【解决方案3】:

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

    str_ireplace(find,replace,string,count)
    

    您的 php 代码将是:

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

    【讨论】:

    • 感谢您的帮助。您的回答最适合我的需求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    相关资源
    最近更新 更多