Page 1 of 1

regex help

Posted: Tue Mar 16, 2004 4:48 pm
by tim
hi guys,

i'm using eregi_replace to find various words (going to be using it for a word filtering set-up) but anyway... Out of curiousty I figured I would use it to replace smiles with images (like this board does)

heres a snipplet of code (all that should be needed):

Code: Select all

<?php
  $code[] = ":)"; 
  $replace[] = "[edited]";

// following is a foreach statement, not needed
?>
so this code picks :) and replaces it with the word [edited] just fine

my problem: if I type in :)) it displays [edited])

I only want it to display the $replace array if its :) and not :)) or :))) or so on.

i tried several attempts but I get no results (as i'm just learning regexps), any help would be deeply appreciated. :D

Posted: Tue Mar 16, 2004 4:54 pm
by phuts
can you just insert a space in the searched-for string, like so:

Code: Select all

$code[] = ":) ";

Posted: Tue Mar 16, 2004 5:05 pm
by Illusionist
and a space before!

Code: Select all

$code[] = "  ";

Posted: Tue Mar 16, 2004 5:31 pm
by phuts
although I was just thinking, what if someone ends a sentence in a smilie followed by some punctuation

in that case, perhaps you should search for the string ":)" and in your foreach statement, set it up so that it replaces $code[] with $replace[] unless the following character is also a ")"

too bad i'm not much help.. I don't really know how to go about that.. anyone?

Posted: Tue Mar 16, 2004 5:37 pm
by PrObLeM
try this

$body = str_replace(":)", "[editied]", $body);
$body = str_replace(":(", "[sad]", $body);

ect.

Posted: Tue Mar 16, 2004 6:25 pm
by redmonkey
Personally, I don't use ereg as I don't think it is as powerful or as quick as preg, so I don't know if it is possible to do what you want using eregi. I would use preg_replace like this....

Code: Select all

<?php

$text = 'Here is a :) but we don''t want this :)) and certainly not this :)))) but this :) is OK';

$search  = '/:\)(?!\))/';
$replace = '[edited]';

$text = preg_replace($search, $replace, $text);

echo $text;

?>

Posted: Tue Mar 16, 2004 7:07 pm
by tim
PrObLeM wrote:try this

$body = str_replace(":)", "[editied]", $body);
$body = str_replace(":(", "[sad]", $body);

ect.
I know i could do that. 2 things why I dont want to:
a.)i am using this method as word filtering system as well, str_replace is case sensisitve, eregi is not.
b.)i am trying to learn regular expressions

I did not know it would pick up the spaces, nifty. however like phuts said, what if the sentence was ended by the face. red no offense, but i'm not that good with expressions(yet) to understand yours. if you could break down the abc's for me, i would be delighted.

how come this dont work:
$code[] = "[ |]:)[ |]";

dont that mean a space OR no space before and after within the [] brakets? doing it like this i would to tpye (space):)(space)
(space) being an actual space to get the [edited].

i'm just trying to learn here, thanks for the help.

Posted: Tue Mar 16, 2004 7:29 pm
by tim
making a lil advancement.

this code works regardless if a space is present or not (both before or after):

Code: Select all

<?php
$code[] = " ?:) ?"; 
?>
however, it consumes the actual space when displayed. ie:

hi this is a :)
would output:
hi this is a[edited]

i want it to output
hi this is a [editied]

how can I get it to not replace the space?

Posted: Tue Mar 16, 2004 7:45 pm
by tim
this works, but i'm not happy with it:
$code[] = ":)";
$replace[] = " [edited] ";

still open for suggestions using the desired method all u regEP gurus

Posted: Tue Mar 16, 2004 7:46 pm
by redmonkey
tim,

The leading and trailing slashes are the delimiters, you can think of them as a container for the actual expression. For the purpose of breaking down the expression these can be ignored.

So, :\) this is the actual smiley you are looking for, the slash is there as the ) character has special meaning with regex so we are required to escape it.

Next we have (?!\)) the outer bracket set is the container for the assertion. And that leaves us with ?!\) in PCRE the combination of ?! basically means 'not followed by'. So in this case ?!\) means not followed by the ) character, again, we escape the ) character as this also has special meaning within the epression.
tim wrote: how come this dont work:
$code[] = "[ |]:)[ |]";

dont that mean a space OR no space before and after within the [] brakets?
No, the square bracket defines a character class, you would need to define a sub-pattern for the space or not part, the ? character is used to denote 'zero or one' so in your case the expression should be..

$code[] = " ?:\)";

However, that would mean you will also be replacing the space (if it exists), so you would need to define the space as a sub-pattern and your replacement would be requied to use the 'backreference' to also add the space if it were there.

So it would look like this...

$code[] = "( ?):\)";
$replace[] = '\\1[edited]';

Posted: Tue Mar 16, 2004 7:59 pm
by tim
wow...

thank you bunches red, that has helped me greatly in both understanding and completing my pet project.

but:
$search = '/:\)(?!\))/';

Warning: eregi_replace(): REG_BADRPT in /home/tim/public_html/... on line 9

btw, if one day your bored, could i get you on AOL or icq or something and have you fling a few examples at me/explain whats happening? seems you know alot and i done read 2 tutorials and your thread has helped me more those two combined.

btw, line 9 is:
$string = eregi_replace($value, $replace[$key], $string);

Posted: Tue Mar 16, 2004 8:16 pm
by redmonkey
I'm glad you are making progress.
tim wrote: but:
$search = '/:\)(?!\))/';

Warning: eregi_replace(): REG_BADRPT in /home/tim/public_html/... on line 9
This is a preg not ereg pattern, if you reffer to my code example you will see that I have used the preg_replace function not eregi_replace.

As I said previously, I don't use ereg so I'm not sure if there is a comparable ereg pattern for that particular one.

My first code example was to deal with your original problem of replacing :) but not :)) or :)))

I don't use AOL or icq as it generally leads to a rather non-productive day. But feel free to shoot me a PM, if I have the time I will reply.

Posted: Tue Mar 16, 2004 8:57 pm
by redmonkey
tim,

I've just re-read your post...
tim wrote: btw, line 9 is:
$string = eregi_replace($value, $replace[$key], $string);
If you haven't got this one worked out yet, can you post the full code, as I don't see anything wrong with line 9 as such but you may have something a miss in $value or $replace[$key], full code would help.

Posted: Wed Mar 17, 2004 5:19 pm
by tim
redmonkey wrote:I'm glad you are making progress.

This is a preg not ereg pattern, if you reffer to my code example you will see that I have used the preg_replace function not eregi_replace.
I changed it to preg_replace and it works fine.. i'm a newbie with expressions so I had no clue, again

thanks muchos for the help red.