futrose Posted September 18, 2012 Share Posted September 18, 2012 I have this bit of code that was originally ereg but needed changed to preg_match. I'm just not sure where to put the delimiter in these two lines. $ee[$i]=preg_match('-','/',$ee[$i]); and $es[$N]=preg_match('-','/',$es[$N]); thanks. Quote Link to comment https://forums.phpfreaks.com/topic/268505-changing-to-preg_match-question/ Share on other sites More sharing options...
cyberRobot Posted September 18, 2012 Share Posted September 18, 2012 I would recommend str_replace() since it doesn't require regular expressions: http://php.net/manual/en/function.str-replace.php Quote Link to comment https://forums.phpfreaks.com/topic/268505-changing-to-preg_match-question/#findComment-1378862 Share on other sites More sharing options...
futrose Posted September 19, 2012 Author Share Posted September 19, 2012 thanks, I went with str_replace and it got rid of the errors. I now have this line $body=preg_replace("[date]",$showDateS,$body); on the output it is placing the "[" and "]" around my date. How do I make that go away? Quote Link to comment https://forums.phpfreaks.com/topic/268505-changing-to-preg_match-question/#findComment-1379147 Share on other sites More sharing options...
Psycho Posted September 19, 2012 Share Posted September 19, 2012 thanks, I went with str_replace and it got rid of the errors. I now have this line $body=preg_replace("[date]",$showDateS,$body); on the output it is placing the "[" and "]" around my date. How do I make that go away? if you have the [] as part of the text to find it shouldn't retain those characters when it is replaced. Show the part of the text that contains '[date]' as well as what the value of $showDateS is. Quote Link to comment https://forums.phpfreaks.com/topic/268505-changing-to-preg_match-question/#findComment-1379161 Share on other sites More sharing options...
cyberRobot Posted September 19, 2012 Share Posted September 19, 2012 $body=preg_replace("[date]",$showDateS,$body); on the output it is placing the "[" and "]" around my date. How do I make that go away? You're actually using preg_replace() which uses regular expressions. Basically, the square quotes in "[date]" are being used as the delimiter. So they're not technically part of the search. Instead, try str_replace(): <?php $body = str_replace("[date]", $showDateS, $body); ?> Quote Link to comment https://forums.phpfreaks.com/topic/268505-changing-to-preg_match-question/#findComment-1379228 Share on other sites More sharing options...
futrose Posted September 19, 2012 Author Share Posted September 19, 2012 ahhhh... nice. Changing to str_replace took care of the the "[]" problem. Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/268505-changing-to-preg_match-question/#findComment-1379234 Share on other sites More sharing options...
cyberRobot Posted September 19, 2012 Share Posted September 19, 2012 No problem For what it's worth, the regular expression would have needed to look something like the code below. The "/" character is the delimiter. Square brackets need to be escaped with "\" since they have special meaning in regular expressions. <?php $body = preg_replace("/\[date\]/", $showDateS, $body); ?> Note: str_replace() is a better option in this case since it doesn't require the power of regular expressions. Quote Link to comment https://forums.phpfreaks.com/topic/268505-changing-to-preg_match-question/#findComment-1379235 Share on other sites More sharing options...
Psycho Posted September 19, 2012 Share Posted September 19, 2012 You're actually using preg_replace() which uses regular expressions. Basically, the square quotes in "[date]" are being used as the delimiter. So they're not technically part of the search. I don't know what I was thinking. I followed the post about using str_replace() and totally blew it on recognizing that preg_replace() was being used. BUt, now that I think about it, the square brackets should not have worked as delimiters. The delimiter is supposed to be the same character. A left and right bracket are different characters. Odd. Quote Link to comment https://forums.phpfreaks.com/topic/268505-changing-to-preg_match-question/#findComment-1379248 Share on other sites More sharing options...
cyberRobot Posted September 19, 2012 Share Posted September 19, 2012 The delimiter is supposed to be the same character. A left and right bracket are different characters. Odd. Huh, good point. Quote Link to comment https://forums.phpfreaks.com/topic/268505-changing-to-preg_match-question/#findComment-1379249 Share on other sites More sharing options...
Christian F. Posted September 19, 2012 Share Posted September 19, 2012 Note quite. Brackets can also be used as delimiters, and then they do they're used in the open-close positions as they're named. In addition to the aforementioned delimiters, it is also possible to use bracket style delimiters where the opening and closing brackets are the starting and ending delimiter, respectively. {this is a pattern} Quoted from: http://php.net/manual/en/regexp.reference.delimiters.php Quote Link to comment https://forums.phpfreaks.com/topic/268505-changing-to-preg_match-question/#findComment-1379253 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.