Page 1 of 1

I want to show and replace something

Posted: Wed Apr 16, 2008 9:28 am
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!

Re: I want to show and replace something

Posted: Wed Apr 16, 2008 9:36 am
by onion2k
Use str_replace().

url_encode() might be a better option though, but it doesn't do what you're asking for.

Re: I want to show and replace something

Posted: Wed Apr 16, 2008 9:39 am
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!

Re: I want to show and replace something

Posted: Wed Apr 16, 2008 9:55 am
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..

Re: I want to show and replace something

Posted: Wed Apr 16, 2008 9:56 am
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.

Re: I want to show and replace something

Posted: Wed Apr 16, 2008 10:08 am
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

Re: I want to show and replace something

Posted: Wed Apr 16, 2008 10:46 am
by nicosns
I got now this (not working offcourse :oops: )

Code: Select all

<?php $replace = str_replace(" ", "-", "<?= htmlentities($_GET['q']) ?>"); echo $replace; ?>

Re: I want to show and replace something

Posted: Wed Apr 16, 2008 10:55 am
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.

Re: I want to show and replace something

Posted: Wed Apr 16, 2008 11:05 am
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/...

Re: I want to show and replace something

Posted: Wed Apr 16, 2008 11:09 am
by nicosns
I found the solution :D

Code: Select all

<?php $replace = str_replace(" ", "-", "$_GET[q]"); echo $replace; ?>
Thanks!

Re: I want to show and replace something

Posted: Wed Apr 16, 2008 11:22 am
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.