2013-04-14 76 views
-2

處理後取代它,我有這樣的類似如何訪問字符串的特定部分,並在PHP

$text_string = 'Every thing must be done in time. So,It is not a good thing to be 
       so late. What are the Rules of This Process are prominent in this 
       video [VIDEO]http://www.youtube.com/watch?v=yseAuiSl[/VIDEO]. So, 
       It will be more sensible if you watch a tutorial here 
       [VIDEO]http://www.dailymotion.com/video/xyxmu6_underwater[/VIDEO] 
       It is much more explanatory. These are the Rules of Thumb.' 

我需要得到每個[VIDEO] .... [/VIDEO],然後將它傳遞給函數的字符串(我創建我自己該功能),將其轉換成根據其嵌入代碼例如

[VIDEO]http://www.youtube.com/watch?v=yseAuiSl[/VIDEO] 

將被轉換爲

<iframe width="680" height="450" src="http://www.youtube.com/embed/yseAuiSl" 
frameborder="0" allowfullscreen></iframe> 

然後,我需要用其嵌入代碼替換[VIDEO] .... [/VIDEO]。那麼,我如何循環遍歷整個字符串並逐個獲取各個[VIDEO] ... [/VIDEO]標記,並在處理後用它的嵌入代碼替換它?

+0

'preg_replace();'? – egig

+0

@Charlie僅用於替換,但首先我需要獲取整個VIDEO標籤,通過函數處理它,然後將其替換爲輸出。那麼,如何從字符串中獲取每個VIDEO標籤? –

+0

使用'preg_match()' - >處理 - >'preg_replace()'。祝你好運 ! – egig

回答

1

在花了很多時間和在Stackoverflow的幫助下,我已經得到了解決方案。

$text_string = 'Every thing must be done in time. So,It is not a good thing to be 
       so late. What are the Rules of This Process are prominent in this 
       video [VIDEO]http://www.youtube.com/watch?v=yseAuiSl[/VIDEO]. So, 
       It will be more sensible if you watch a tutorial here 
       [VIDEO]http://www.dailymotion.com/video/xyxmu6_underwater[/VIDEO] 
       It is much more explanatory. These are the Rules of Thumb.' 

這裏是將我的鏈接轉換爲嵌入代碼的功能

function convert_to_embed($matches) { 
    $link = $matches[1]; 

    // All the Function Process 

    return $embed; 
} 

這裏我使用preg_replace_callback功能,將通過一個處理每個視頻標籤之一和功能轉換和替代帶有嵌入代碼的VIDEO標籤。

$finalized_string = preg_replace_callback('/\[VIDEO\](.+?)\[\/VIDEO\]/i', "convert_to_embed", $text_string); 
1
echo preg_replace('/\[VIDEO\](.+?)\[\/VIDEO\]/i', '<iframe width="680" height="450" src="\\1" frameborder="0" allowfullscreen></iframe>', $text_string); 
+0

雖然,我必須處理VIDEO標籤之間的鏈接,但我從你的preg_replace了一個想法。我將使用preg_replace_callback。感謝REGEX。 –

0
在我的想法

: 你將需要通過語言來迭代,找到起點和視頻的結束位置; 一些代碼:

$text_string = 'Every thing must be done in time. So,It is not a good thing to be 
       so late. What are the Rules of This Process are prominent in this 
       video [VIDEO]http://www.youtube.com/watch?v=yseAuiSl[/VIDEO]. So, 
       It will be more sensible if you watch a tutorial here 
       [VIDEO]http://www.dailymotion.com/video/xyxmu6_underwater[/VIDEO] 
       It is much more explanatory. These are the Rules of Thumb.' 

$start = '[VIDEO]'; 
$end = '[/VIDEO]'; 
$words_array = explode(' ',$text_string); 
$words = array_flip($words_array); 

//Then you can check for video element with: 
$word_pos = 0; 
foreach($words as $the_word){ 
$word_pos++; 
if ($the_word == $start){ 
$start_point = $word_pos; 
} 

if ($the_word == $end){ 
$end_point = $word_pos; 
} 
} 
$video_link = echo substr($text_string,$start_point,$end_point); 

的代碼只是和大家分享我有這個概念..!

相關問題