wiggly81 Posted August 29, 2008 Share Posted August 29, 2008 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 More sharing options...
discomatt Posted August 29, 2008 Share Posted August 29, 2008 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) Link to comment https://forums.phpfreaks.com/topic/121904-solved-preg_replace-help/#findComment-628991 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 Not in single quotes, discomatt, since escape characters aren't parsed. Link to comment https://forums.phpfreaks.com/topic/121904-solved-preg_replace-help/#findComment-628994 Share on other sites More sharing options...
wiggly81 Posted August 29, 2008 Author Share Posted August 29, 2008 Wow fast reply ty so much this is what i wanted and it seems so simple and i been going nuts for a few days over it :$ $pattern = '/\\(p\\)/si'; // will match (p) again thanx Link to comment https://forums.phpfreaks.com/topic/121904-solved-preg_replace-help/#findComment-629002 Share on other sites More sharing options...
discomatt Posted August 29, 2008 Share Posted August 29, 2008 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. Link to comment https://forums.phpfreaks.com/topic/121904-solved-preg_replace-help/#findComment-629031 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 I completely agree with you that you should, but not inside of a regex. It just makes it look sloppy. Link to comment https://forums.phpfreaks.com/topic/121904-solved-preg_replace-help/#findComment-629034 Share on other sites More sharing options...
kratsg Posted August 29, 2008 Share Posted August 29, 2008 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 Link to comment https://forums.phpfreaks.com/topic/121904-solved-preg_replace-help/#findComment-629125 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.