doubi12138 2011-05-02 00:22
浏览 39
已采纳

如何在php中更改网址的一部分?

I am new to PHP and I am trying to work on this one problem where the middle of a URL changes.

For instance using this fake URL,

$url = 'http://www.bobsplace.com/events/georgetown/12354544233123';

I need to be able to substitute "georgetown" out with a random variable.

So I guess I need to like slice the URL after events, have a variable where "georgetown" would be, and then have the rest of the URL continue after it. I hope I am making as much sense as possible.

Alright, I tried all these and still go errors, but most likely it is due to my current coding. I am entering the entire thing in now.

<?

// This URL only shows future events

$url = 'https://graph.facebook.com/192674347443031/events?access_token=153420758043439|66d692f0e73ad17d939d9d9c-1045402872|8VHLfMb3gYY0p9fnT7wMS8Q9Krc&expires_in=0&since=yesterday' ;

$event_id = get_post_custom_values('facebook_id');
$url = str_replace('/192674347443031/', '/' . $event_id . '/', $url);


$json = file_get_contents($url) ;

$data = json_decode( $json,true) ; 
//var_dump($data) ; 

$sorted_array = array_reverse($data['data'], TRUE) ; 

foreach ($sorted_array as $key => $value) {

    //print_r($value) ; 
    ?>

<? echo "<li><a href='http://facebook.com/event.php?eid=". $value['id']  . "' target='_blank'>" ;  
                                        echo "<h2>" .  $value['name'] . "</h2>" ;
                                        echo "</a>" ;
                                        echo " (" . $value['location'] .") <br />" ; 
                                        echo date("F j, Y, g:i a" , strtotime($value['start_time'])) . " to " . date("F j, Y, g:i a" , strtotime($value['end_time'])) ;?>
                                    <div class="moreinfo">    
                                     <? echo "<a href='http://facebook.com/event.php?eid=". $value['id']  . " ' target='_blank'>" ; 
                                        echo "<b>" .  'More info' . "</b>" ;
                                        echo "</a>" ;?>
                                        </div>
                                        <? echo "</li>"; 


}



?>

The errors I keep getting are now saying my array_reverse() should be an array. And also invalid argument foreach()

Now this is not the entire code on that page, but this is the snippet that is giving me problems.

  • 写回答

3条回答 默认 最新

  • dongtaijiao7140 2011-05-02 00:24
    关注

    I think you're trying to say that you want to replace the text "georgetown". If that's correct, the following code should work (make sure you've defined $replace to what you want to replace it with):

    $url = str_replace('/georgetown/', '/' . $replace . '/', $url);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置