Julius Posted March 10, 2012 Share Posted March 10, 2012 Heres what I have so far: $url = str_replace ( "/index.php/", "", $_SERVER [ 'REQUEST_URI' ] ); $url = preg_replace('#([^A-Za-z0-9_-])#', '', $url); echo $url; I want to replace EVERYTHING in my url EXCEPT a-zA-Z0-9_- and forward slash. How can I escape forward slash in my pattern? Quote Link to comment https://forums.phpfreaks.com/topic/258662-replace-everything-except/ Share on other sites More sharing options...
JAY6390 Posted March 10, 2012 Share Posted March 10, 2012 You can just put the forward slash since you're using # as your delimiter Quote Link to comment https://forums.phpfreaks.com/topic/258662-replace-everything-except/#findComment-1325960 Share on other sites More sharing options...
ragax Posted March 10, 2012 Share Posted March 10, 2012 Hi Julius, In agreement with Jay. A touch more compact than what you have at the moment: $url = preg_replace('#[^/[:alnum:]_-]#','',$url); Wishing you a fun weekend. [Edit: I had forgotten the hyphen and underscore] Quote Link to comment https://forums.phpfreaks.com/topic/258662-replace-everything-except/#findComment-1325965 Share on other sites More sharing options...
Julius Posted March 10, 2012 Author Share Posted March 10, 2012 I was playing around with this pattern for about an hour, and the thing that I did wrong was adding slash right before ], not after ^. But what works faster: :alnum: or all given values? or it doesn't really matter in this case? Thank you for your help, guys! Quote Link to comment https://forums.phpfreaks.com/topic/258662-replace-everything-except/#findComment-1325969 Share on other sites More sharing options...
ragax Posted March 10, 2012 Share Posted March 10, 2012 But what works faster: :alnum: or all given values? Running this, I'm not seeing a difference. $start=time(); $string='http://www.whatever.com?12()$%^*&'; for ($i=0;$i<1000000;$i++) $url=preg_replace('#[^/[:alnum:]_-]#','',$string); $lap=time(); for ($i=0;$i<1000000;$i++) $url=preg_replace('#[^/0-9a-zA-Z_-]#','',$string); $end=time(); $time1= $lap - $start; $time2= $end - $lap; echo $time1."<br />"; echo $time2."<br />"; Output: 6 6 Quote Link to comment https://forums.phpfreaks.com/topic/258662-replace-everything-except/#findComment-1325973 Share on other sites More sharing options...
Julius Posted March 10, 2012 Author Share Posted March 10, 2012 Thank you very much for your help Quote Link to comment https://forums.phpfreaks.com/topic/258662-replace-everything-except/#findComment-1325974 Share on other sites More sharing options...
ragax Posted March 10, 2012 Share Posted March 10, 2012 You're welcome, Jay had it right, wishing you a fun weekend. Quote Link to comment https://forums.phpfreaks.com/topic/258662-replace-everything-except/#findComment-1325978 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.