Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何访问字符串的特定部分并在php中处理后替换它_Php - Fatal编程技术网

如何访问字符串的特定部分并在php中处理后替换它

如何访问字符串的特定部分并在php中处理后替换它,php,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]

我有一串这样的东西

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

echo preg\u replace('/\[VIDEO\](.+?)\[\/VIDEO\]/i',''.$text\u字符串);
echo preg_replace('/\[VIDEO\](.+?)\[\/VIDEO\]/i', '<iframe width="680" height="450" src="\\1" frameborder="0" allowfullscreen></iframe>', $text_string);
在我看来: 你需要反复阅读单词,找到视频的开始和结束位置; 一些代码:

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

该代码只是为了分享我的概念,这

花了很多时间,在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\u replace\u callback
函数,该函数将逐个处理每个视频标记,该函数将转换视频标记并用其嵌入代码替换

 $finalized_string  = preg_replace_callback('/\[VIDEO\](.+?)\[\/VIDEO\]/i', "convert_to_embed", $text_string);

@Charlie,这只是为了替换,但是首先我需要得到整个视频标签,通过一个函数处理它,然后用它的输出替换它。那么,如何从字符串中提取每个视频标记呢?使用
preg\u match()
->处理->
preg\u replace()
。祝你好运google:php替换标签虽然,我必须处理视频标签之间的链接,但是我从你的preg_替换中得到了一个想法。我将使用preg_replace_回调。谢谢你的正则表达式。