如何在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.

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);

you can do :

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

$url = str_replace("georgetown", $my_var, $url );

or :

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

$temp = explode( '/'  , $url );
$temp[4] = $my_var;
$url = implode( '/' , $temp );

First one will replace by name , second one by position in URL.

$variable = 'place'; //e.g. georgetown
$url = 'http:www.bobsplace.com/events/'.$variable.'/12354544233123';

I think this is what you're asking for.