Jump to content

changing to preg_match question


futrose

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.