Jump to content

replace everything except...


Julius
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.