Search code examples
javascriptphpformsyoutubetrim

Trimming Form Field Data


I have an html form field on my webpage for users to submit YouTube 'share' video links.

However, I only want a portion of the link passed.

When using the YouTube 'copy and paste''share' option, their URL's all seem to start with:

https://youtu.be/

What I would like to do (If possible), is when my users enter the full URL...

For Example: https://youtu.be/TP8RB7UZHKI

I would only like the: TP8RB7UZHKI to show up on the php results page.

I would like the https://youtu.be/ portion of the URL to always be omitted (Trimmed off of the beggining).

I could instruct my site visitors do it when filling out the form, but that might be confusing to most of them, and I can't afford the mistakes.

I have included a very stripped down version of the html form and the php results page codes below.

Again... I don't know if this is even possible, but if so, I would appreciate advice and/or an example on how to achieve this.

The Form Page:

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

The Video URL Upload Result PHP Page:

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

    <body>

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

    </body>
    </html>

Solution

  • You can use str_ireplace() in php to replace https://youtu.be/ with empty string. Its syntax:

    str_ireplace(find,replace,string,count)
    

    Your php code will be:

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