The14thGOD Posted June 22, 2009 Share Posted June 22, 2009 I've looked for a while and cant seem to find how to escape < and > out of my regular expression. <?php $row['body'] = preg_replace("\<p\>","<p>$image01<strong>$row[display_date]</strong> - ",$row['body'],1);?> I've tried single '\' and i get an error and i've tried double \\. Obviously I don't know much about reg expressions, sorry. I rarely use them and when I do it's usually copy/paste for URLs and Emails. Quote Link to comment Share on other sites More sharing options...
ldougherty Posted June 22, 2009 Share Posted June 22, 2009 From http://www.php.net/manual/en/function.preg-replace.php#49362 The following should be escaped if you are trying to match that character \ ^ . $ | ( ) [ ] * + ? { } , Special Character Definitions \ Quote the next metacharacter ^ Match the beginning of the line . Match any character (except newline) $ Match the end of the line (or before newline at the end) | Alternation () Grouping [] Character class * Match 0 or more times + Match 1 or more times ? Match 1 or 0 times {n} Match exactly n times {n,} Match at least n times {n,m} Match at least n but not more than m times More Special Character Stuff \t tab (HT, TAB) \n newline (LF, NL) \r return (CR) \f form feed (FF) \a alarm (bell) (BEL) \e escape (think troff) (ESC) \033 octal char (think of a PDP-11) \x1B hex char \c[ control char \l lowercase next char (think vi) \u uppercase next char (think vi) \L lowercase till \E (think vi) \U uppercase till \E (think vi) \E end case modification (think vi) \Q quote (disable) pattern metacharacters till \E Even More Special Characters \w Match a "word" character (alphanumeric plus "_") \W Match a non-word character \s Match a whitespace character \S Match a non-whitespace character \d Match a digit character \D Match a non-digit character \b Match a word boundary \B Match a non-(word boundary) \A Match only at beginning of string \Z Match only at end of string, or before newline at the end \z Match only at end of string \G Match only where previous m//g left off (works only with /g) I do not believe the < > needs to be escaped, \ or \\ should have worked. What error do you get when your escaping? Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted June 22, 2009 Author Share Posted June 22, 2009 Error: Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in index.php on line 377 Hmm, when i leave them unescaped i get the same characters at the start and end of the string. so basically it looks like: < $image01<strong>$row[display_date]</strong> - > I assumed it just replaced the letter p. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 23, 2009 Share Posted June 23, 2009 <?php $row['body'] = preg_replace("#<p>#i","<p>$image01<strong>$row[display_date]</strong> - ",$row['body'],1);?> Quote Link to comment Share on other sites More sharing options...
.josh Posted June 23, 2009 Share Posted June 23, 2009 The problem is that regex expects a starting and ending delimiter so it knows where the start and end of the pattern is. Since <...> can be used as delimiters, without escaping them, preg_replace looks at them as such, therefore making 'p' the only thing in your actual pattern. But if you escape them, you no longer have delimiters. So you have two choices: a) use them as delimiters and escape the ones in your actual pattern, like so: '<\<p\>>' b) choose a different delimiter (see Ken's post for example) Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted June 23, 2009 Author Share Posted June 23, 2009 thank you ken for the fix and thank you crayon for explaining it, though I'm a little confused about the 'i' at the end... again, thanks for the help! Quote Link to comment Share on other sites More sharing options...
.josh Posted June 23, 2009 Share Posted June 23, 2009 i is a modifier which means to make the pattern matching case insensitive. Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted June 23, 2009 Author Share Posted June 23, 2009 ah, thanks! i really need to sit down and get my head wrapped around these, i have good links, just not to time Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.