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