dtt78245 2016-09-22 05:26
浏览 42
已采纳

在嵌入式视频代码上使用preg_match来提取其id URL

I will be using a form to input embedded video code into a form field so it can be stored into a MYSQL database. The iframe scrolling, width, height, frame border, and src should not be stored in the database. How can I use the php preg_match function to remove iframe scrolling, width, height, frame border,and src out of embedded video code. I only want the URL id to video. How can I store only the link to the embedded video and not all its attributes? This is the code:

<?php
$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

if(!mysqli_select_db($conn,'tutorial'))
{
echo 'Database not selected';
}

$title = test_input($_POST['title']);
$embedded_video = test_input($_POST['embedded_video']);

//I want preg_match to remove, src, iframe scrolling, width, height, and frameborder out of embeded code

if (preg_match(%^(src)"/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$embedded_video)) {

      //puting the URL of video in database
      $sql = "INSERT INTO person(title,embedded_video) VALUES ('$title', '$embedded_video')";
    }    
  }


if(!mysqli_query($conn, $sql))
{
 echo "Not inserted";
}
else {

 echo "INSERT";

}



function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}


?>

<!DOCTYPE html>
<html>
<body>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  <input type="text" name="title" >
  <br>
  Embedded video:<br>
  <input type="text" name="embedded_video">
  <br><br>
  <input type="submit" value="Insert">
</form> 


</body>
</html>
  • 写回答

1条回答 默认 最新

  • douji2283 2016-09-22 12:04
    关注

    You can give preg_match() a third variable, an array(). This will store the match on the first index and any part matches in the other indices.

    I think this will be your solution:

    if (preg_match(%^(src)"/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $embedded_video, $matches)) {
    
        //puting the URL of video in database
        $sql = "INSERT INTO person(title,embedded_video) VALUES ('" . $title . "', '" . $matches[0] . "')";
    }
    

    (You used an } too much after this if statement!)

    For further reference: http://php.net/manual/en/function.preg-match.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化