Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:41:36+00:00 2026-06-15T18:41:36+00:00

Why doesn’t preg_replace return anything in this scenario? I’ve been trying to figure it

  • 0

Why doesn’t preg_replace return anything in this scenario? I’ve been trying to figure it out all night.

Here is the text contained within $postContent:

Test this. Here is a quote: [Quote]1[/Quote] Quote is now over.

Here is my code:

echo "Test I'm Here!!!";
                    $startQuotePos = strpos($postContent,'[Quote]')+7;
                    $endQuotePos = strpos($postContent,'[/Quote]');
                    $postStrLength = strlen($postContent);
                    $quotePostID = substr($postContent,$startQuotePos,($endQuotePos-$postStrLength));
                    $quotePattern = '[Quote]'.$quotePostID.'[/Quote]';
                    $newPCAQ = preg_replace($quotePattern,$quotePostID,$postContent);
                    echo "<br />$startQuotePos<br />$endQuotePos<br />$quotePostID<br />Qpattern:$quotePattern<br />PCAQ: $newPCAQ<br />";

This is my results:

Test I’m Here!!!

35

36

1

Qpattern:[Quote]1[/Quote]

PCAQ:

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:41:37+00:00Added an answer on June 15, 2026 at 6:41 pm

    For preg_replace(), "[Quote]" matches a single character that is one of the following: q, u, o, t, or e.

    If you want that preg_replace() finds the literal "[Quote]", you need to escape it as "\[Quote\]". preg_quote() is the function you should use: preg_quote("[Quote]").

    Your code is also wrong because a regular expression is expected to start with a delimiter. In the preg_replace() call I am showing at the end of my answer, that is @, but you could use another character, as long as it doesn’t appear in the regular expression, and it is used also at the end of the regular expression. (In my case, @ is followed by a pattern modifier, and pattern modifiers are the only characters allowed after the pattern delimiter.)

    If you are going to use preg_replace(), it doesn’t make sense that you first find where "[Quote]" is. I would rather use the following code:

    $newPCAQ = preg_replace('@\[Quote\](.+?)\[/Quote\]@i', '\1', $postContent);
    

    I will explain the regular expression I am using:

    • The final '@i' is saying to preg_replace() to ignore the difference between lowercase, and uppercase characters; the string could contain "[QuOte]234[/QuOTE]", and that substring would match the regular expression the same.

    • I use a question mark in "(.+?)" to avoid ".+" is too greedy, and matches too much characters. without it, the regular expression could include in a single match a substring like "[Quote]234[/Quote] Other text [Quote]475[/Quote]" while this should be matched as two substrings: "[Quote]234[/Quote]", and "[Quote]475[/Quote]".

    • The '\1' string I am using as replacement string is saying to preg_replace() to use the string matched from the sub-group "(.+?)" as replacement. In other words, the call to preg_replace() is removing "[Quote]", and "[/Quote]" surrounding other text. (It doesn’t replace "[/Quote]" that doesn’t match with "[Quote]", such as in "[/Quote] Other text [Quote]".)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why doesn't this work? type RetryBuilder(max) = member x.Return(a) = a // Enable 'return'
Doesn't seem to be anything about this online, I need to run fetch query
Why doesn't this: def subsets(x): if not x: return [[]] else: return [x +
Why doesn't this work: <Style TargetType=s:Substance> <Setter Property=Template> <Setter.Value> <DataTemplate> <StackPanel> <TextBlock Text={TemplateBinding Name}/>
Why doesn't this code work? I'am using FF. <head> <script type=text/javascript> document.getElementById(someID).onclick = function(){
Why doesn't this print out true? > Dim test1 As Decimal? = Nothing >
This doesn't occur in Chrome. I am trying to implement an Ellipsis for the
Why doesn't following code print anything: #!/usr/bin/python3 class test: def do_someting(self,value): print(value) return value
Why doesn't this work? <script type=text/javascript src=js/jquery-1.8.1.js></script> <script type=text/javascript> $(document).ready(function() { $(body).html($(body).html().replace(/&reg;/g, '<sup>&reg;</sup>').replace(/®/g, '<sup>&reg;</sup>').replace(\u00AE
Doesn't value have to return toString() to be able to call value.toString()? When do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.