Jump to content

changing to preg_match question


futrose

Recommended Posts

  Quote

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
$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);
?>

No problem 8)

 

 

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

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.

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.

  Quote
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

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.