如何在特定字符之前插入链接

时间:2015-12-30 09:52:12

标签: php string

这是视频块文件我从file_get_contents得到这个并将它存储在我的变量中现在我想在这个字符串中做一些更改我想把我的url放在所有

之前
  <磷>氮_

像这样开始每个n_行

  

http://example.com/n_1_0_0.ts?nimblesessionid=9142491

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:8
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:5.005,
#EXTINF:5.005,

n_1_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_2_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_3_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_4_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_5_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_6_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_7_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_8_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_9_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_10_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_11_0_0.ts?nimblesessionid=9142491

2 个答案:

答案 0 :(得分:3)

非常简单的任务 - 使用preg_replace()

<?php
$txt = '#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:8
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:5.005,
#EXTINF:5.005,

n_1_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_2_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_3_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_4_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_5_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_6_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_7_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_8_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_9_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_10_0_0.ts?nimblesessionid=9142491
#EXTINF:7.507,
n_11_0_0.ts?nimblesessionid=9142491';

// ^ means from beginning of string
// m modifier is using for multi line string - its combine with ^ modifier
// 
$txt = preg_replace("/^(n_)+/m",'http://example.com/$1',$txt);
#$txt = preg_replace("/^n_/m",'http://example.com/n_',$txt);
echo $txt;

答案 1 :(得分:-1)

$oldstr = 'http://example.com/n_','n_1_0_0.ts?nimblesessionid=9142491'
$newstr = str_replace('n_', 'http://example.com/n_',$oldstr);

str_replace

这将用http://example.com/n_

替换n_个子串

如果您的链接在一个数组中(它们应该是),那么只需循环:

$newstrings = array();
foreach($oldstrings as $string) {
    $newstrings[] = str_replace('n_', 'http://example.com/n_',$string);
}