himtuna Posted March 21, 2009 Share Posted March 21, 2009 Hi I need help with the php code <?php $current_url = "http://" .$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI']; print $current_url ?> this is gonna return a url ( a string ) with exactly this pattern ( so dont have to worry much about the logics fo the url returned) http://yoursite.com/node/123/idontwant_thisstuff or could be http://yoursite.com/?q=node/123/idontwant_thisstuff Basically i want to run a loop to get rid of "/idontwant_thisstuff" i.e anything after node/87/... should get deleted. i.e i just want url two main things first the word "node"( ofcourse the base url also http://yoursite.com/ ) and the numeric value proceeding it. eg http://yoursite.com/node/1/events ---> http://yoursite.com/node/1 http://yoursite.com/node/123423423/groups -----> http://yoursite.com/node/123423423 please if someone could do this Having working since morning on drupal, and this is the last step(except some css work) to achieve my goals. please help me out Thanks Himtuna Quote Link to comment https://forums.phpfreaks.com/topic/150468-need-string-formatted/ Share on other sites More sharing options...
wildteen88 Posted March 21, 2009 Share Posted March 21, 2009 Something like preg_match('@^/([a-z]+)/([0-9]+)@i', $_SERVER['REQUEST_URI'], $matches); $url = "http://" .$_SERVER['HTTP_HOST'].$matches[0]; print $url; Quote Link to comment https://forums.phpfreaks.com/topic/150468-need-string-formatted/#findComment-790299 Share on other sites More sharing options...
himtuna Posted March 21, 2009 Author Share Posted March 21, 2009 Thanks this is real awesome code but a little problem( but once i move my site online this will be rectified as I dont have clean urls) is that ?= appears in the query just before the 'node' so its not giving the desired results. Drupal is not going to generate anything other than 'node' as a first argument. A drupal guy gave me this code( it uses drupal variables) <?php if ( arg(0) == 'node' && is_numeric(arg(1)) ) { $my_id = arg(1); echo $my_id ; } ?> But your solution has opened my mind, so hardcore programmer. Thanks buddy Quote Link to comment https://forums.phpfreaks.com/topic/150468-need-string-formatted/#findComment-790323 Share on other sites More sharing options...
himtuna Posted March 21, 2009 Author Share Posted March 21, 2009 This is the final working snippet <?php if ( arg(0) == 'node' && is_numeric(arg(1)) ) { $my_id = arg(1); print url('node/' . $my_id . '/somestring', array('absolute' => TRUE)); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150468-need-string-formatted/#findComment-790381 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.