Help - Search - Members - Calendar
Full Version: modify a piece of a string.
Pixel2Life Forum > Help Section > PHP, ASP, MySQL, JavaScript and other Web/Database Programming Help
Jynxis
What I am trying to accomplish is this: When my site hits a 404 page, i have a search box that will contain certain parts of the url (keywords) in a text field. What I'm having trouble with right now is how to take the piece with the certain file extensions and then remove the extension. but leave any periods within the string intact, other than the ending period before the extension.

I thought of using preg_match and eregi but I'm not sure how to go about it. I've haven't coded in PHP in over a few years so a lot of the knowledge has been forgotten.

This is what i have so far..
CODE
<?php
//http://mywebsite.com/cat/sub/file.php
            $querys = explode("/",$_SERVER['REQUEST_URI']);            
            foreach($querys as $q_item){
                if(eregi("(.*).php?",$q_item)){
                    $term = explode(".",$q_item);
                    
                    $search_terms .= $term[0] . ": ";    
                }else{
                    $search_terms .= $q_item  . " ";    
                }
            }
        ?>
Output:cat sub file


I've tried looking it up on google and and this site, but i haven't found anything in quite a while.
Any help and advice would be appreciated. Also any recommendations on good regex sites would be a plus.
Jynxis
i think i figured it out but if anyone has a better way don't hesitate.
CODE
$querys = explode("/",$_SERVER['REQUEST_URI']);            
            foreach($querys as $q_item){
                if(preg_match("/(.+?)\.php/", $q_item, $matches)){
                        $search_terms .= $matches[1] . " ";
                }else{
                    $search_terms .= $q_item  . " ";    
                }
            }
Hayden
What exactly are you trying to get out of the URL? It looks like you're just trying to get the filename, if that's it then you could do it with basename().

CODE
$query = str_replace('.php','',basename($_SERVER['REQUEST_URI']));
Jynxis
Yes i could have done it with basename. But there were problems with it.

Essentially what i'm trying to do is take from the query bar.
Once i've taken the string and chopped it up, i then check for anything ending in .php and remove it. (now yes i could have done basename, but im actually trying to catch more than just (.php)) Then i check for a few variables in the query string and output them.

The semi completed code. Which works just fine.
CODE
$querys = explode("/",$_SERVER['REQUEST_URI']);
            $querys = array_reverse($querys); // Technically not needed anymore... but its there. for theres sake.
             $search_terms = array();
            foreach($querys as $q_item){
                if(preg_match("/(.+?)\.(php|html)/", $q_item, $matches)){
                        if($q_item != ""   && $q_item != "404.php") $search_terms[] = $matches[1] . "";
                }elseif(preg_match("/(.+?)\?.*/", $q_item, $matches)){
                        if($q_item != "")
                        $search_term_q .= $matches[0] . "";
                        $q =  explode("?",$matches[0]);
                        $search_term_q  .= $q[1];
                }else{
                    if($q_item != ""  && $q_item != "404")$search_terms[] = $q_item  . "";    
                }
            }
            $search_terms = str_replace("%20", "+",$search_terms);
            $search_terms = implode(" ", $search_terms);
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.