V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
YAFEIML
V2EX  ›  PHP

小白来问个 PHP 的问题好吗

  •  
  •   YAFEIML · 2014-10-23 15:00:57 +08:00 · 3437 次点击
    这是一个创建于 3474 天前的主题,其中的信息可能已经有所发展或是发生改变。
    $bt ="http://www.xdytt.com/wp-content/uploads/2014/07/0e1f7d6029a030b3.torrenthttp://www.xdytt.com/wp-content/uploads/2014/07/8af4e5816cc01962.torrenthttp://www.xdytt.com/wp-content/uploads/2014/07/0e1f7d6029a030b3.torrenthttp://www.xdytt.com/wp-content/uploads/2014/07/8af4e5816cc01962.torrenthttp://www.xdytt.com/wp-content/uploads/2014/07/0e1f7d6029a030b3.torrenthttp://www.xdytt.com/wp-content/uploads/2014/07/8af4e5816cc01962.torrent"

    怎样格式化这些内容呢,不一定是这么多啊,可能少点,也可能多点,格式化成这样行不
    <a href="http://www.xdytt.com/wp-content/uploads/2014/07/0e1f7d6029a030b3.torrent">0e1f7d6029a030b3</a>
    <a href="http://www.xdytt.com/wp-content/uploads/2014/07/0e1f7d6029a030b3.torrent">0e1f7d6029a030b3</a>
    <a href="http://www.xdytt.com/wp-content/uploads/2014/07/0e1f7d6029a030b3.torrent">0e1f7d6029a030b3</a>
    <a href="http://www.xdytt.com/wp-content/uploads/2014/07/0e1f7d6029a030b3.torrent">0e1f7d6029a030b3</a>
    <a href="http://www.xdytt.com/wp-content/uploads/2014/07/0e1f7d6029a030b3.torrent">0e1f7d6029a030b3</a>

    程序不熟悉,缺少技能和思路的说啊
    26 条回复    2014-10-24 18:33:57 +08:00
    zakokun
        1
    zakokun  
       2014-10-23 15:08:05 +08:00
    最简单的办法就是, $arr=explode("torrent",$bt); 再把分割出来的每个数组元素末尾拼接上"torrent"就行了
    zakokun
        2
    zakokun  
       2014-10-23 15:09:09 +08:00
    @zakokun 这样出来的结果就是href里面的连接. 之后你再在页面模板里面该怎么遍历就怎么弄了
    pimin
        3
    pimin  
       2014-10-23 15:11:32 +08:00
    字符串分隔成数组,
    YAFEIML
        4
    YAFEIML  
    OP
       2014-10-23 15:13:25 +08:00
    @zakokun 那个,,这个,多码点好吗
    66beta
        5
    66beta  
       2014-10-23 15:13:32 +08:00
    正则表达式可以一次提取吧,但是我不会写
    lizheming
        6
    lizheming  
       2014-10-23 15:32:08 +08:00   ❤️ 2
    YAFEIML
        7
    YAFEIML  
    OP
       2014-10-23 16:12:09 +08:00
    @lizheming 拜谢
    abelyao
        8
    abelyao  
       2014-10-23 16:21:41 +08:00   ❤️ 1
    简单点的:

    echo '<a href="' . join('.torrent">download</a> <a href="http://', explode('.torrenthttp://', $bt)) . '">download</a>';

    利用 expload 分割字符串再用 join 组合起来,但是每个 a 标签中的文本内容,就只能是固定的。
    如果要匹配的话,可参考楼上 @lizheming 贴的代码,利用正则表达式提取。
    YAFEIML
        9
    YAFEIML  
    OP
       2014-10-23 16:25:01 +08:00
    @abelyao 这个的话如果只有一条数据就死了吧。。
    abelyao
        10
    abelyao  
       2014-10-23 16:35:43 +08:00
    @YAFEIML 链接是活的~ 显出出来的文本是死的~
    YAFEIML
        11
    YAFEIML  
    OP
       2014-10-23 17:01:23 +08:00
    @abelyao
    我的意思不是那个
    explode('.torrenthttp://', $bt)

    如果只有一条记录.torrenthttp://这个取不到吧
    abelyao
        12
    abelyao  
       2014-10-23 17:29:41 +08:00
    @YAFEIML 哦,也是,那还是用正则吧,或者按 http:// 来分隔,但注意去掉第一个结果,因为是空白字符串
    yangg
        13
    yangg  
       2014-10-23 17:35:57 +08:00
    第一个
    var_dump(preg_split('/(?<=.torrent)(?=http)/', $bt));
    YAFEIML
        14
    YAFEIML  
    OP
       2014-10-23 18:02:40 +08:00
    @abelyao
    @yangg
    其实已经用@lizheming方法弄好了,谢谢
    lygmqkl
        15
    lygmqkl  
       2014-10-23 19:26:57 +08:00
    用 http explode 就行了。。。简单的方法最好。
    karonl
        16
    karonl  
       2014-10-23 22:16:04 +08:00   ❤️ 1
    @lizheming 好奇你是怎么找到和题目一模一样的网页,而楼主却没找到
    lizheming
        17
    lizheming  
       2014-10-23 22:18:21 +08:00
    @karonl ...擦...为什么你不能觉得是我写的呢T_T...惨惨惨...
    YAFEIML
        18
    YAFEIML  
    OP
       2014-10-24 09:44:27 +08:00
    @karonl
    @lizheming
    好萌噢
    YAFEIML
        20
    YAFEIML  
    OP
       2014-10-24 11:06:16 +08:00
    @good 和@lizheming 那个差不多吧。。
    karonl
        21
    karonl  
       2014-10-24 12:56:49 +08:00
    @lizheming
    @YAFEIML
    噢,没注意看
    good
        22
    good  
       2014-10-24 16:58:57 +08:00
    @YAFEIML 恩 发了之后才看到的 他的用了闭包 - -
    YAFEIML
        23
    YAFEIML  
    OP
       2014-10-24 17:34:26 +08:00
    @lizheming
    刚发现memcached开启后只显示第一条,,,
    lizheming
        24
    lizheming  
       2014-10-24 17:41:34 +08:00
    @YAFEIML ??????不明白什么情况咧...
    YAFEIML
        25
    YAFEIML  
    OP
       2014-10-24 17:55:34 +08:00
    @lizheming 额,就是开了memcached后不循环了。。
    lizheming
        26
    lizheming  
       2014-10-24 18:33:57 +08:00
    @YAFEIML 不懂=w=..检查下你的$bt变量输出吧,函数肯定没啥问题。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1039 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:03 · PVG 07:03 · LAX 16:03 · JFK 19:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.