tags:

views:

90

answers:

4

What I want


Hi! I want to replace :) to smile.png and :)) to laugh.png.

The problem


The script finds :) inside :)) so laugh.png is not shown, only smile.png+)

This is what I have tried so far for :)):


preg_replace("/:)+(?!))/i",$image, $string))

Some other regexes what I tried:

"/\:\)+(?=\))/i"

"/\:+(?=\)\))/i"

But nothing want's to work what I tried yet.

A: 

Why don't you do a simple text replace?

First :)) to laungh.png, then :) to smile.png

Adam
I tried, but I don't know why doesn't works, maybe I could try on a new and clean page :)
CIRK
+2  A: 
str_replace(array(":))", ":)"), array("laugh.png", "smile.png"), $string);

The order is important.

Artefacto
+1 What's with the downvotes? It's a perfectly valid solution, and it's better than using regex. You don't need regex for this.
NullUserException
+5  A: 

For :) – (:\)(?!\)))

Then

For :)) – (:\)\))

hookedonwinter
First answer that actually answered the op. Funny how many people avoid answering when they see regexp's.
Blindy
My bad for not wrapping in `/.../`.. But it works. Tested on rubular.com
hookedonwinter
Blindy, unless the op stated that he really wanted a regexp solution, shouldn't we help by posting a better solution?
Adam
Thanks works perfectly, :D
CIRK
@Blindly That's because using a regular expression here hinders the readability and is unnecessarily complex (not to mention less efficient).
Artefacto
Cool thanks for rubular.com I was searching for hours for a service like this, but most of them sucks...
CIRK
@Blindy although I see quite a few questions that are asking for regexps and shouldn't be using them. this one seems fine though.
xenoterracide
@Adam, I see a regexp tag and regexp is mentioned in the title. What more do you want?
Blindy
By the way the regex is not even correct. The first one will fail if `:)` is the last thing. It should use negative lookahead or `|$`. But you were very quick to upvote it and downvote the correct solutions, even if they're a better solution for this problem.
Artefacto
@Artefacto true! What's the fix?
hookedonwinter
@Adam I'm more just curious how to make the regex work. I'd use his or Rick's if it were my code.
hookedonwinter
@hookedonwinter Sorry, I mistook you for the op.
Adam
I fixed it and also removed the unnecessary escaping of `:`.
Artefacto
Why are these expressions wrapped in parentheses? It is entirely unnecessary (and less readable here!) to use capture groups for a whole expression, since PHP regex functions already provide `$0` instead.
Peter Boughton
What do you mean @Peter Boughton? sry I'm new with regex :S
CIRK
@Peter Good question. No real reason.
hookedonwinter
@Artefacto thanks! the.more.you.know.
hookedonwinter
+2  A: 
$string = str_replace(':))', 'laugh.png', $string);
$string = str_replace(':)', 'smile.png', $string);

php.net on str_replace: "If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace()."

Rick