2016-05-14 26 views
2

我正在爲我的網站編寫一個簡單的博客,並且我已成功完成此操作。使用PHP自動檢測並在HTML內容中嵌入MP4/MP3鏈接

function convertYoutube($string, $id='') { 
     return preg_replace(
      "/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i", 
      "<div class=videoWrapper><iframe style=\"clear:both;\" width=\"420\" height=\"315\" src=\"//www.youtube.com/embed/$2\" allowfullscreen></iframe></div> 
      ", 
      $string 
     ); 
    } 

這是將youtube網址(短或長)轉換爲嵌入表單。

現在我希望做的是這樣的:

  • 轉換的MP4鏈接,如:
    http://cdn.example.com/akndh39/2016/askbdsjdbuu/my-video.mp4?id=28gsbd0mu&d=33hfsbb

    <video width="320" height="240" controls> <source src="movie.mp4" type="video/mp4"> </video>

  • 轉換MP3鏈接到相應的代碼格式

不過,我不想鏈接轉換,如本

​​

我希望他們保持不變一樣,對於MP4和MP3的鏈接。

編輯

我想是這樣的:

This is a very nice Video by Artiste So-n-So https://scontent.cdninstagram.com/t50.2886-16/13229628_1095069080586346_1424116369_n.mp4 

<a href="http://cdn.example.com/akndh39/2016/askbdsjdbuu/my-video.mp4?id=28gsbd0mu&d=33hfsbb"> 
Download Video</a> 

enter image description here

This is a very nice Video by Artiste So-n-So  <video width="320" height="240" controls> 
 
<source src="https://scontent.cdninstagram.com/t50.2886-16/13229628_1095069080586346_1424116369_n.mp4" type="video/mp4"> 
 
</video> 
 

 
<a href="http://cdn.example.com/akndh39/2016/askbdsjdbuu/my-video.mp4?id=28gsbd0mu&d=33hfsbb"> 
 
Download Video</a>

+0

請發佈**輸入**和所需**輸出**的清晰示例。 –

+0

我編輯了問題 – Normal9ja

+0

你會怎麼從* * to * Download Video *? – sweaver2112

回答

3

此正則表達式會發現MP4的網址:

(["\']?https?:\/\/[^\/]+(?:\/[^\/]+)*?.mp4\?(&?\w+(=[^&\'"\s]*)?)*) 

之後有一些PHP代碼來替換與視頻和源代碼網址。請注意,它現在捕獲前導引號字符以確定該URL是否包含在href=""src=""鏈接中;下面的代碼明確檢查並跳過發現以引號字符開頭的URL。查看第一個示例中的第二個URL,瞭解它是如何工作的。

下面是使用URL來從文本解析獨立MP4網址示例代碼:

$subject = <<<LABEL 
    This is a very nice Video by Artiste So-n-So https://s...content-available-to-author-only...m.com/t50.2886-16/13229628_1095069080586346_1424116369_n.mp4 


     <a href="http://c...content-available-to-author-only...e.com/akndh39/2016/askbdsjdbuu/my-video.mp4?id=28gsbd0mu&d=33hfsbb"> 
     Download Video</a>" 
LABEL; 

$subject = <<<LABEL 
Please one more thing, can you expand your regex to include URLs like these. 
https://z-1-scontent-lhr3-1.xx.fbcdn.net/v/t42.1790-4/13226507_264732453878764_‌​308543824_n.mp4?efg=eyJ2ZW5jb2RlX3RhZyI6InN2ZV9zZCJ9&oh=c68eadae7d9a5b25ad290ea72‌​3f8fc40&oe=57378FA2 
that is URLs that doesn't end immediately with the extension, but have other parameters added to it. 
LABEL; 

$pattern = '(["\']?https?:\/\/[^\/]+(?:\/[^\/]+)*?.mp4\?(&?\w+(=[^&\'"\s]*)?)*)'; 
$matches = Array(); 
$match = preg_match($pattern, $subject, $matches); 

if (count($matches) !== 0) { 
    $first_char = substr($matches[0], 0, 1); 
    if ($first_cahr !== '"' && $first_char !== "'") { 
     $replace = '<video width="320" height="240" controls><source src="' . $matches[0] . '" type="video/mp4"></video>'; 

     $result = str_replace($matches[0], $replace, $subject); 

     print_r($result); 
    } 
} 

用於第一測試情況下的輸出是:

Success time: 0.03 memory: 52480 signal:0 
    This is a very nice Video by Artiste So-n-So <video width="320" height="240" controls><source src="https://scontent.cdninstagram.com/t50.2886-16/13229628_1095069080586346_1424116369_n.mp4" type="video/mp4"></video> 


     <a href="http://cdn.example.com/akndh39/2016/askbdsjdbuu/my-video.mp4?id=28gsbd0mu&d=33hfsbb"> 
     Download Video</a>" 

輸出的第二測試情況下是:

Please one more thing, can you expand your regex to include URLs like these. 
<video width="320" height="240" controls><source src="https://z-1-scontent-lhr3-1.xx.fbcdn.net/v/t42.1790-4/13226507_264732453878764_‌​308543824_n.mp4?efg=eyJ2ZW5jb2RlX3RhZyI6InN2ZV9zZCJ9&oh=c68eadae7d9a5b25ad290ea72‌​3f8fc40&oe=57378FA2" type="video/mp4"></video> 
that is URLs that doesn't end immediately with the extension, but have other parameters added to it. 
+0

好的,我會嘗試一下並回複評論。 – Normal9ja

+0

對不起,但這並沒有真正奏效。 – Normal9ja

+0

這是什麼意思?它處理樣本輸入,所以如果它與其他輸入不同,那麼你能讓我知道它是什麼以便我可以修復它嗎?我是唯一一個甚至試圖給你一個答案,所以請幫助我來幫助你。 –