wolfeblog Posted January 13, 2007 Share Posted January 13, 2007 I have the solution to my problem; I just don't know why it works and that disturbs me.I used to write a lot of C/ASM code; I'm rusty now. I'm totally new to PHP. I want to remove all instances of <!--word--> from $content and add a preface string, $preface.So, it seems[b]$content = $preface . preg_replace("<!--word-->", "", $content); [/b]will suffice. It doesn't.This changes "<!--word-->" into "<>".Messing about, I find that:[b]$content = $preface . preg_replace("^!--word-->^", "", $content); [/b]works fine. I believe the ^ operator is a delimiter that means 'new line'. Why adding this would fix it (since there's not a new line at that point in $content) I have no clue. Any insights/advice? And, especially, why doesn't the first version work?Thanks very much in advance,-wolfe Link to comment https://forums.phpfreaks.com/topic/34018-newbie-question-on-delimiters-in-preg_replace/ Share on other sites More sharing options...
ShogunWarrior Posted January 13, 2007 Share Posted January 13, 2007 I don't think [b]^[/b] means anything in this context. In PHP all regexps must have a beginning and ending delimeter. Link to comment https://forums.phpfreaks.com/topic/34018-newbie-question-on-delimiters-in-preg_replace/#findComment-159946 Share on other sites More sharing options...
utexas_pjm Posted January 13, 2007 Share Posted January 13, 2007 Preg replace works with Perl'esque regex so you'll want a pair of //'s around your pattern. I believe the following regex will work as expected:[code]$content = $preface . preg_replace("/(<!--word-->)/", "", $content); [/code]As a side note, if you are only replacing the static string "<!--word-->", then str_replace will do the job more efficiently.[code]$content = $preface . str_replace("<!--word-->", "", $content); [/code]Best,Patrick Link to comment https://forums.phpfreaks.com/topic/34018-newbie-question-on-delimiters-in-preg_replace/#findComment-159949 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.