By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,746 Members | 1,924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,746 IT Pros & Developers. It's quick & easy.

str_replace quirk

P: n/a
Jon
Why does the following not replace the ":)"?
str_replace(":)", "&nbsp;<img
src=http://www.com.com/forum/emoticons/smiley.gif>&nbsp;", $_POST['Tip']);

But, the following does replace the ":("?

str_replace(":(", "&nbsp;<img
src=http://www.com.com/forum/emoticons/crying.gif>&nbsp;", $_POST['Tip']);
--
--------
Jon Rosenberg
Jul 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Jon
Forgot to add....I am running PHP 4.3.0.
"Jon" <ru*******@hotmail.com> wrote in message
news:vk************@corp.supernews.com...
Why does the following not replace the ":)"?
str_replace(":)", "&nbsp;<img
src=http://www.com.com/forum/emoticons/smiley.gif>&nbsp;", $_POST['Tip']);

But, the following does replace the ":("?

str_replace(":(", "&nbsp;<img
src=http://www.com.com/forum/emoticons/crying.gif>&nbsp;", $_POST['Tip']);
--
--------
Jon Rosenberg

Jul 16 '05 #2

P: n/a
Jon
Duh, stupid me. Thanks for pointing that out to me.
"Damian Brown" <da****@phpexpert.org> wrote in message
news:bh**********@titan.btinternet.com...
jon,

it is because of the closed bracket
the closed bracket in str_replace(":)"
tells the interpreter that that is the end of the statement
it works with :( because it does not take it as the end of the statement

i'm not sure, but you may have to put a backslash before it i.e. \)

Regards,
Damian
www.phpexpert.org/chat/
UK FREEphone 0800 019 0924
"Jon" <ru*******@hotmail.com> wrote in message
news:vk************@corp.supernews.com...
Why does the following not replace the ":)"?
str_replace(":)", "&nbsp;<img
src=http://www.com.com/forum/emoticons/smiley.gif>&nbsp;", $_POST['Tip']);
But, the following does replace the ":("?

str_replace(":(", "&nbsp;<img
src=http://www.com.com/forum/emoticons/crying.gif>&nbsp;", $_POST['Tip']);

--
--------
Jon Rosenberg


Jul 16 '05 #3

P: n/a
it was good to be of assistance
you can reach me on CHAT
at www.phpexpert.org/chat/
if you have any further problems

Regards,
Damian
www.phpexpert.org
UK FREEphone 0800 019 0924

"Jon" <ru*******@hotmail.com> wrote in message
news:vk************@corp.supernews.com...
Duh, stupid me. Thanks for pointing that out to me.
"Damian Brown" <da****@phpexpert.org> wrote in message
news:bh**********@titan.btinternet.com...
jon,

it is because of the closed bracket
the closed bracket in str_replace(":)"
tells the interpreter that that is the end of the statement
it works with :( because it does not take it as the end of the statement

i'm not sure, but you may have to put a backslash before it i.e. \)

Regards,
Damian
www.phpexpert.org/chat/
UK FREEphone 0800 019 0924
"Jon" <ru*******@hotmail.com> wrote in message
news:vk************@corp.supernews.com...
Why does the following not replace the ":)"?
str_replace(":)", "&nbsp;<img
src=http://www.com.com/forum/emoticons/smiley.gif>&nbsp;", $_POST['Tip']);
But, the following does replace the ":("?

str_replace(":(", "&nbsp;<img
src=http://www.com.com/forum/emoticons/crying.gif>&nbsp;", $_POST['Tip']);

--
--------
Jon Rosenberg



Jul 16 '05 #4

P: n/a
Jon
You're right...and I solved it by not bothering with str_replace and just
made a regex using preg_replace. I'm not sure exactly what was going on
with str_replace...but, after playing with it more, it does not seem to be
the ).

Here's the simple regex I used:
$post = preg_replace("/\:\)/", "&nbsp;<img
src=http://www.com.com/forum/emoticons/smiley.gif>&nbsp;", $post);

$post = preg_replace("/\;\)/", "&nbsp;<img
src=http://www.com.com/forum/emoticons/wink.gif>&nbsp;", $post);

$post = preg_replace("/\:\(/", "&nbsp;<img
src=http://www.com.com/forum/emoticons/crying.gif>&nbsp;", $post);
"Jochen Buennagel" <za**@buennagel.com> wrote in message
news:bh*************@news.t-online.com...
Damian Brown wrote:
it is because of the closed bracket
the closed bracket in str_replace(":)"
tells the interpreter that that is the end of the statement
it works with :( because it does not take it as the end of the statement


Sorry, but the closing bracket has nothing to do with it because it is
enclosed in "", thus it is part of the string.

As an aside: the code works nicely for me as it is. (Running PHP 4.3.2)

Jochen

--
/**
* @author Jochen Buennagel <zang at buennagel dot com>
* @see http://www.sourceforge.net/projects/zang
*/

Jul 16 '05 #5

P: n/a
i'm glad you got it going
i'm no expert, i'm just learning

"Jon" <ru*******@hotmail.com> wrote in message
news:vk************@corp.supernews.com...
You're right...and I solved it by not bothering with str_replace and just
made a regex using preg_replace. I'm not sure exactly what was going on
with str_replace...but, after playing with it more, it does not seem to be
the ).

Here's the simple regex I used:
$post = preg_replace("/\:\)/", "&nbsp;<img
src=http://www.com.com/forum/emoticons/smiley.gif>&nbsp;", $post);

$post = preg_replace("/\;\)/", "&nbsp;<img
src=http://www.com.com/forum/emoticons/wink.gif>&nbsp;", $post);

$post = preg_replace("/\:\(/", "&nbsp;<img
src=http://www.com.com/forum/emoticons/crying.gif>&nbsp;", $post);
"Jochen Buennagel" <za**@buennagel.com> wrote in message
news:bh*************@news.t-online.com...
Damian Brown wrote:
it is because of the closed bracket
the closed bracket in str_replace(":)"
tells the interpreter that that is the end of the statement
it works with :( because it does not take it as the end of the
statement
Sorry, but the closing bracket has nothing to do with it because it is
enclosed in "", thus it is part of the string.

As an aside: the code works nicely for me as it is. (Running PHP 4.3.2)

Jochen

--
/**
* @author Jochen Buennagel <zang at buennagel dot com>
* @see http://www.sourceforge.net/projects/zang
*/


Jul 16 '05 #6

P: n/a
Damian Brown wrote:
i'm just learning


We all are. The minute you think you have nothing left to learn about a
language is the exact time your programming career starts to end.

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 16 '05 #7

P: n/a
Jon wrote:
Why does the following not replace the ":)"?
str_replace(":)", "&nbsp;<img
src=http://www.com.com/forum/emoticons/smiley.gif>&nbsp;", $_POST['Tip']);

But, the following does replace the ":("?

str_replace(":(", "&nbsp;<img
src=http://www.com.com/forum/emoticons/crying.gif>&nbsp;", $_POST['Tip']);

Try using single quotes instead - if you're not interpolating a variable,
it's MUCH better to use ' instead of ", since there's no need for the parser
to check the contents of the string for variable names.

The other point, is that str_replace doesn't change the string directly - it
returns the result of the replace, so you'd need to do

$_POST['Tip'] = str_replace(':)', '&nbsp;<img src="http://www.com.com/icons/crying.gif" alt="Crying Smiley" />&nbsp;', $_POST['Tip']);

or something similar - remember
1 - attribute values should be quoted (src="this.gif")
2 - img tags should be closed (<img src=..... />)
3 - add an alt attribute, so that the img means something even if people don't/can't
display images; and so that this will help you towards writing valid code, depending
on your variant of html/xhtml

Matt

--
Matt Mitchell - AskMeNoQuestions
Dynamic Website Development and Marketing
Jul 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.