-2

我有一串这样的

$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]标签并在处理后将其替换为其嵌入代码?

4

3 回答 3

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 标签,并且该函数将转换并用其嵌入代码替换 VIDEO 标签。

 $finalized_string  = preg_replace_callback('/\[VIDEO\](.+?)\[\/VIDEO\]/i', "convert_to_embed", $text_string);
于 2013-04-14T06:39:28.287 回答
1
echo preg_replace('/\[VIDEO\](.+?)\[\/VIDEO\]/i', '<iframe width="680" height="450" src="\\1" frameborder="0" allowfullscreen></iframe>', $text_string);
于 2013-04-14T06:10:59.930 回答
0

在我的想法中:您将需要遍历单词并找到 VIDEO 的开始和结束位置;一些代码:

$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);

代码只是为了分享我对此的概念..!

于 2013-04-14T06:18:49.117 回答