Jump to content

Recommended Posts

dtdetu, I mean no offense when I say this, but I would strongly recommend taking some time to learn regex basics. It isn't hard to learn per say.. a little intimidating at first, but once you get past the basics, you realise how useful regex can be.

Link to comment
https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716660
Share on other sites

And if all you want is the breakdown of the url, you can use parse_url instead:

 

example:

 

$str = 'index.php?id=12';
$parse = parse_url($str);
echo '<pre>'.print_r($parse, true);

 

Output:

Array
(
    [path] => index.php
    [query] => id=12
)

Link to comment
https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716670
Share on other sites

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
https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716678
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
https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716690
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
https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716693
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
https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716725
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
https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716757
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.