Jump to content


modify a piece of a string.


3 replies to this topic

#1 Jynxis

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:The Shadows

Posted 18 October 2009 - 12:11 AM

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..
<?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.

Edited by Jynxis, 18 October 2009 - 03:33 AM.


#2 Jynxis

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:The Shadows

Posted 18 October 2009 - 03:40 AM

i think i figured it out but if anyone has a better way don't hesitate.
$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  . " ";	
				}
			}

Edited by Jynxis, 18 October 2009 - 03:42 AM.


#3 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 18 October 2009 - 01:47 PM

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().

$query = str_replace('.php','',basename($_SERVER['REQUEST_URI']));


#4 Jynxis

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:The Shadows

Posted 22 October 2009 - 01:24 PM

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.
$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);






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users