Jump to content

[SOLVED] preg_replace help!!


wiggly81

Recommended Posts

hi ya,

i am relativly new to PHP and i have stumbled on a problem and was hoping someone here could help..

ok so the issue i have, i am writing a forum and currently am writing some bbcode...

 

its emotes i am having an issue with.

 

$str = preg_replace('/:\-\#/si', '<img src="./image/emote/shhh.gif" border="0" />', $str);

that line works fine but when i try to do the same with round bracketts it does not work, so i guess i have the wrong idea..

str = preg_replace('/(\p\)/si', '<img src="./image/emote/pic.gif" border="0" />', $str);

all i get there is a bunch of error msgs.

 

i know i can do this with str_replace() however that requires me to enter each line more than once due to case sensative, i am running PHP 4 so str_ireplace() is not a option.

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/121904-solved-preg_replace-help/
Share on other sites

What exactly do you want to replace? Brackets are a special character in regex, so they must be escaped with a backslash, which also should be escaped in PHP strings

$pattern = '/\\(p\\)/si'; // will match (p)

$pattern = '/\\(\\\\p\\)/si'; // will match (\p)

Not in single quotes, discomatt, since escape characters aren't parsed.

 

Actually, the backslash is STILL and escape character in single quotes...

 

echo 'can\'t touch this!'; # Outputs: can't touch this

echo 'escaped \\ backslash'; # Outputs: escaped \ backslash

 

Though it's not necessary due to PHP's 'looseness', it's still considered good practise to escape a backslash in single quotes.

Note, keep this for future reference... The following characters would have to be escaped in order to be treated as literals.

 

\  the escape character - used to find an instance of a metacharacter like a period, brackets, etc.

. (period) match any character except newline

x match any instance of x

^x match any character except x

[x] match any instance of x in the bracketed range - [abxyz] will match any instance of a, b, x, y, or z

| (pipe) an OR operator - [x|y] will match an instance of x or y

() used to group sequences of characters or matches

{} used to define numeric quantifiers

{x} match must occur exactly x times

{x,} match must occur at least x times

{x,y} match must occur at least x times, but no more than y times

? preceding match is optional or one only, same as {0,1}

* find 0 or more of preceding match, same as {0,}

+ find 1 or more of preceding match, same as {1,}

^ match the beginning of the line

$ match the end of a line

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.