Jump to content

[SOLVED] easy replace


dtdetu

Recommended Posts

i used this code and works , incase someone needs.. thanks

   $str = $_SERVER['REQUEST_URI'];
     $parse = parse_url($str);
       $parse[path]=str_replace('/','',$parse[path]);
       $parse[path]=str_replace('.php','',$parse[path]);
     return $parse[path];

Link to comment
Share on other sites

thank you all , there is too much to learn and not enough time thanks

 

Poor excuse which can only last so long... No one has all the time to learn.. we make the time to do so. Otherwise, you are at the mercy of everyone else.. a pity really. Don't be lazy.

Link to comment
Share on other sites

See, now that I see you post a code snippet, I can see you are making effort.. good for you.

 

Here is my take on this:

 

$str = 'index.php?id=12';
$parse = parse_url($str);
preg_match('#^([^.]+)#', $parse['path'], $match);
echo $match[1];

 

Output:

index

Link to comment
Share on other sites

yes but if we dont use parse url then it gets the ?id=2 part too and other $_GETs

 

It should work.. here is the breakdown of the preg:

 

'#^/([^.]+).*#', "$1", $str

 

Basically, this is saying: At the start of the string ( noted by the ^ character), after the /, capture anything that is not a dot, and replace the entire thing with just what was captured.. so if I were to use '/someFile.php?id=7' to pretend it is the end results of $_SERVER['REQUEST_URI'], I plug this into the last snippet example like so:

 

$str = '/someFile.php?id=7';
$str = preg_replace('#^/([^.]+).*#', "$1", $str);
echo $str;

 

The regex will only use what it captured.. so in this case, the initial /, and everything (including and after) the dot character is replaced with what is inside those brackets.. so in the above case, the result would be

 

someFile

 

Try it out.

Link to comment
Share on other sites

You know what? Even easier (now that I think about it.. [man I must be brain dead today]), is to use pathinfo like so:

 

$str = $_SERVER['REQUEST_URI'];
$x = pathinfo($str);
echo $x[filename];

 

So let's pretend $_SERVER['REQUEST_URI'] = /someFile.php?id=7, once again, the end result will be

 

someFile

 

I think this snippet is the best way to go.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.