Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

Preg_replace help needed! Lol

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
SpaceshipSpaceship
Watch

PoorDoggie

Soon to be RICHdoggie!VIP Member
Impact
18
this dosen't seem to work:
PHP:
$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
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
do the urlencoding after of the preg_replace, not before! I believe that's right.

:)
 
0
Close
will have a go... stand by

nope - even as:
PHP:
$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?
 
0
Close
Please help... anyone!
 
0
Close
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
 
0
Close
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:
$old_q = $_GET['q'];

to

PHP:
$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.
 
1
Close
aha! works - You are a lifesaver! :lol:

have some rep
mikesherov said:
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:
$old_q = $_GET['q'];

to

PHP:
$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.
 
0
Close
CatchedCatched

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back