I want to show and replace something

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nicosns
Forum Newbie
Posts: 21
Joined: Thu Jun 21, 2007 1:53 pm

I want to show and replace something

Post by nicosns »

Hello,

My visitors can use the search on my website. Now when people do a search, for example webmaster stuff, they get the url http://www.mysite.com/search.php?q=webmaster+stuff

With

Code: Select all

<?= htmlentities($_GET['q']) ?></a>
I show webmaster stuff on the page.

With the same code I also create an url

Code: Select all

http://www.mysite.com/more.php?id=<?= htmlentities($_GET['q']) ?>
to let them look in another database.

And here comes the problem:
The last code turns in http://www.mysite.com/more.php?id=webmaster stuff with a space between two words. How can I replace a space with -, so I can show the link http://www.mysite.com/more.php?id=webmaster-stuff? Thanks!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: I want to show and replace something

Post by onion2k »

Use str_replace().

url_encode() might be a better option though, but it doesn't do what you're asking for.
nicosns
Forum Newbie
Posts: 21
Joined: Thu Jun 21, 2007 1:53 pm

Re: I want to show and replace something

Post by nicosns »

onion2k wrote:Use str_replace().

url_encode() might be a better option though, but it doesn't do what you're asking for.
How can I use this with

Code: Select all

<?= htmlentities($_GET['q']) ?>
?? I'm not a PHP expert, so if someone want to post the solution, many thanks!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: I want to show and replace something

Post by onion2k »

nicosns wrote:
onion2k wrote:Use str_replace().

url_encode() might be a better option though, but it doesn't do what you're asking for.
How can I use this with

Code: Select all

<?= htmlentities($_GET['q']) ?>
?? I'm not a PHP expert, so if someone want to post the solution, many thanks!
I know this is a bit of a crazy 'out there' idea, but you could try learning..
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: I want to show and replace something

Post by pickle »

FYI, you'll rarely get someone on these boards to "post the solution". We prefer to help you help yourself.

Look at the documentation for str_replace() - it's pretty straightforward.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nicosns
Forum Newbie
Posts: 21
Joined: Thu Jun 21, 2007 1:53 pm

Re: I want to show and replace something

Post by nicosns »

I understand, and I want to learn from a solution. I tried alot of things with the code, but I always get an error. Maybe today is my lucky day? :P
nicosns
Forum Newbie
Posts: 21
Joined: Thu Jun 21, 2007 1:53 pm

Re: I want to show and replace something

Post by nicosns »

I got now this (not working offcourse :oops: )

Code: Select all

<?php $replace = str_replace(" ", "-", "<?= htmlentities($_GET['q']) ?>"); echo $replace; ?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: I want to show and replace something

Post by pickle »

Take out the <?= ?> around your htmlentities() call. You're already in a PHP block, you can't/don't need to open another one.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nicosns
Forum Newbie
Posts: 21
Joined: Thu Jun 21, 2007 1:53 pm

Re: I want to show and replace something

Post by nicosns »

pickle wrote:Take out the <?= ?> around your htmlentities() call. You're already in a PHP block, you can't/don't need to open another one.

Code: Select all

<?php $replace = str_replace(" ", "-", "htmlentities($_GET['q'])"); echo $replace; ?>
Now it gives me the error

Code: Select all

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/...
nicosns
Forum Newbie
Posts: 21
Joined: Thu Jun 21, 2007 1:53 pm

Re: I want to show and replace something

Post by nicosns »

I found the solution :D

Code: Select all

<?php $replace = str_replace(" ", "-", "$_GET[q]"); echo $replace; ?>
Thanks!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: I want to show and replace something

Post by pickle »

htmlentities() is a function - don't quote it.

Putting single quotes around array keys when the array is in double quotes will give you that error.

Also, posting your PHP code in

Code: Select all

 
tags rather than

Code: Select all

 
tags makes it much easier to read & pick out possibly syntax errors.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply