我是新来的PH.我有js文件看起来像
var sites = [
//Texte anime
{url:" http://carouselinfo.com/canal/", duration:9},
//Texte anime
{url:" http://carouselinfo.com/canal-2/", duration:9},
//Intro detallee
{url:"http://carouselinfo.com/index-t1.html", duration:35},
//CTA
{url:"http://carouselinfo.com/index-t2.html", duration:35},
//Football
{url:"http://carouselinfo.com/twitter-ligue/", duration:100},
//Texte anime
{url:" http://carouselinfo.com/canal-2/", duration:9},
//TrailersClub.com
{url:"http://trailersclub.com/?lang=fr", duration:480},
//Heure
{url:"http://carouselinfo.com/heure", duration:8},
//Meteo
{url:"http://carouselinfo.com/meteo", duration:12},
//Texte anime
{url:" http://carouselinfo.com/canal-cine/", duration:9},
//Cine
{url:"http://carouselinfo.com/cine/index-t1.html", duration:150},
//Texte anime
{url:" http://carouselinfo.com/canal-2/", duration:9},
//Heure
{url:"http://carouselinfo.com/heure", duration:8},
];我想在var site=之后添加文本[或者任何行,比如使用php在4行中添加文本,所以我们如何才能add.Here是我的代码来替换任何行上的文本。
$lines = array();
foreach( file('source.js') as $line ) {
if ( 'var site=[' === $line ) {
array_push($lines, 'test');
}
array_push($lines, $line);
}
file_put_contents('source.js', $lines);这个代码不能正常工作。
发布于 2012-05-13 14:56:28
如果这就是你想要做的,这将会被附加到你的文件中。
<?
$file = "/path/to/file.js";
$lines = file($file);
$lines[] = "test\n";
file_put_contents($file, $lines);
?>如果您想替换该位置中的文本,您可以直接
foreach($lines as $i => $line)
{
if($line == "text to replace")
$lines[$i] = "replacement";
}如果要在第4行放置新链接,请将数组向右移动并写入数组位置3 ($lines[3])。或者你可以使用我的函数:
function array_insert($array, $index, $value) //assumes all keys are ints
{
for($i=sizeof($array); $i < $index ; $i--)
$array[$i+1] = $array[$i];
$array[$index] = $value;
}如果想要在索引4:array_insert($array, 4, "Test")处插入"test“,请使用如下函数
我还没有测试过,但它应该可以工作。
https://stackoverflow.com/questions/10569852
复制相似问题