dsiv4041 2017-04-16 14:19
浏览 20
已采纳

如何不用PHP脚本创建新行

With the script below, I create links from a text file you, but when I create files, I can line added at the beginning and one at the end of lines, no way to prevent that from happening? The first line of file.txt and Live1 and the last line of https://www.example.com/example/playlist.m3u8 but when I do the transformation in link.txt file, the first and last lines are empty, I would avoid creating empty rows, It's possible? where am I wrong?

This is file.txt

Live1 
https://www.example.com/example/playlist.m3u8
Live2 
https://www.example.com/example/playlist.m3u8
Live3 
https://www.example.com/example/playlist.m3u8
Live4 
https://www.example.com/example/playlist.m3u8
Live5 
https://www.example.com/example/playlist.m3u8

This is the script php

<?php
$file = "file.txt";
$fr = fopen($file, 'r');
$i = $x = 0;
$links = array();
while (!feof($fr)) {
    $riga = trim(fgets($fr));
    if (!empty($riga)) {
        if ($i % 2 == 0) {
            $links[$x]['name'] = $riga;
        } else {
            $links[$x]['link'] = $riga;
            $x++;
        }
        $i++;
    }
}
fclose($fr);

$output = "";
foreach ($links as $link) {
    $output .= "<a href='" . $link['link'] . "' target='blank'>" . $link['name'] . "
 " ;
$file = "link.txt";
$codice = "
{$output}
";
$fo = fopen($file, "w");
chmod($file, 0755);
fwrite($fo, $codice);
fclose($fo);
}

This is the result link.txt

<a href='https://www.example.com/example/playlist.m3u8'>Live1
<a href='https://www.example.com/example/playlist.m3u8'>Live2
<a href='https://www.example.com/example/playlist.m3u8'>Live3
<a href='https://www.example.com/example/playlist.m3u8'>Live4
<a href='https://www.example.com/example/playlist.m3u8'>Live5
  • 写回答

1条回答 默认 最新

  • duanguanzai6181 2017-04-16 14:23
    关注

    You're explicitly creating new lines:

    $codice = "
    {$output}
    ";
    

    If you don't want them, get rid of them:

    $codice = "{$output}";
    

    (Or just use the $output variable directly, since $codice isn't actually doing anything.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题