[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 05-11-2006, 01:07 PM   #1 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice


preg_replace help needed! lol

this dosen't seem to work:
PHP Code:
$old_q = urldecode($_GET['q']);
    
$spelling = "<b>Spelling Suggestion:</b> <a href=\"search.php?".preg_replace("/q=".urlencode($old_q)."/","q=".urlencode($result['ResultSet']['Result']),$_SERVER['QUERY_STRING'])."\">Did you mean <i><b>".$result['ResultSet']['Result']."</b></i></a><br /><br />";
This is a part of the spell checking script on 6YD search.

Can anyone help me? If $_GET['q'] is one word, then the spell check works a dream - ie: http://www.6yd.net/search.php?q=scool&m=web but when you have two words - ie: http://www.6yd.net/search.php?q=scool+lessons&m=web then it spells it correctly in the link's text, but it dosen't correct it in the url. The link and text are in the variable $spelling by the way.

Tom
PoorDoggie is offline  
Old 05-11-2006, 01:52 PM   #2 (permalink)
NamePros Member
 
Join Date: Apr 2005
Posts: 116
134.00 NP$ (Donate)

mikesherov will become famous soon enoughmikesherov will become famous soon enough


do the urlencoding after of the preg_replace, not before! I believe that's right.

:-)
mikesherov is offline  
Old 05-11-2006, 01:54 PM   #3 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice


will have a go... stand by

nope - even as:
PHP Code:
$old_q = $_GET['q'];
$spelling = "<b>Spelling Suggestion:</b> <a href=\"search.php?".preg_replace("/q=$old_q/","q=".urlencode($result['ResultSet']['Result']),$_SERVER['QUERY_STRING'])."\">Did you mean <i><b>".$result['ResultSet']['Result']."</b></i></a><br /><br />";
I still get the same results... The search.php page up on 6yd at the moment has the script above in it, not the origional.

I think it could have something to do with the fact that it is "word+word" in the querystring, but not in a variable or something... anyone got any ideas or a better way of going about it?
PoorDoggie is offline  
Old 05-12-2006, 12:06 PM   #4 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice


Please help... anyone!
PoorDoggie is offline  
Old 05-12-2006, 12:40 PM   #5 (permalink)
NamePros Regular
 
Join Date: Mar 2006
Posts: 394
211.38 NP$ (Donate)

sacx13 is on a distinguished road


Thumbs up Reg ex.

I just tested on my computer and is working in this way:

preg_replace("/q=(.+?)&/",'q='.urlencode($result['ResultSet']['Result'])."&",$_SERVER['QUERY_STRING'])

replace your preg_replace with the above one.

Regards
sacx13 is offline  
Old 05-12-2006, 12:41 PM   #6 (permalink)
NamePros Member
 
Join Date: Apr 2005
Posts: 116
134.00 NP$ (Donate)

mikesherov will become famous soon enoughmikesherov will become famous soon enough


Sorry, I meant you have to url_encode the $_GET[old_q] variable before the replace because the server string will have it encoded, and the get superglobal will have it decoded!

So simply change:
PHP Code:
$old_q = $_GET['q'];
to

PHP Code:
$old_q = urlencode($_GET['q']);
and instead of preg_replace, just use str_replace! It is a bit faster and should be used when you're doing a literal replacement instead of a pattern replacement.
mikesherov is offline  
Old 05-12-2006, 01:15 PM   #7 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice


aha! works - You are a lifesaver!

have some rep
Quote:
Originally Posted by mikesherov
Sorry, I meant you have to url_encode the $_GET[old_q] variable before the replace because the server string will have it encoded, and the get superglobal will have it decoded!

So simply change:
PHP Code:
$old_q = $_GET['q'];
to

PHP Code:
$old_q = urlencode($_GET['q']);
and instead of preg_replace, just use str_replace! It is a bit faster and should be used when you're doing a literal replacement instead of a pattern replacement.
PoorDoggie is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 03:53 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85