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 6553643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:36:23+00:00 2026-05-25T12:36:23+00:00

Not sure how this describe this with a better title, however here is my

  • 0

Not sure how this describe this with a better title, however here is my problem:

i have a replace function with multiple boolean options:

  1. regex
  2. whole words (only when regex==false)
  3. case sensitive

and this means i have to choose 1 of 4 ways to replace my text. Currently my code looks like this:

(those options here are indeed true/false as a string, passed in via POST from a set of jquery checkboxes)

    if($regex=='true')
    {
        if($casesens=='true')
        {
            $p->aData['body'] = preg_replace('/'.$q.'/', $r, $p->aData['body']);
        }
        else
        {
            $p->aData['body'] = preg_replace('/'.$q.'/i', $r, $p->aData['body']);           
        }
    }
    else
    {
        if($wwords=='true')
        {
            $q = " ".$q." ";
            $r = " ".$r." ";
        }
        if($casesens=='true')
        {
            $p->aData['body'] = str_replace($q, $r, $p->aData['body']);
        }
        else
        {
            $p->aData['body'] = str_ireplace($q, $r, $p->aData['body']);
        }
    }

as you can see, if have to compare $casesens in both conditons, and this becomes increasingly complex if i have to add more options to the UI.
Is there a better or more elegant way to write this?

  • 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-05-25T12:36:23+00:00Added an answer on May 25, 2026 at 12:36 pm

    You could simply do the check for $casesens once and define a variable like this:

    if($casesens=='true') {
        $case = 'i';
    }
    

    And then use it in the regex pattern just like any other variable:

    $p->aData['body'] = preg_replace('/'.$q.'/'.$case, $r, $p->aData['body']);
    

    This would solve the first if. As for the second one I can think of 2 ways:

    1. Create a function similar to str_replace and stri_replace that takes an extra boolean argument, to ignore case or not and, call the appropriate string replace function.
    2. You could use preg_replace instead of the string replace functions just like you previously did and use the same way to solve the problem as I explained above.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure how to describe this best. But I have a problem understanding
I'm not sure how to describe this problem, so I think an example is
Not even sure if this is feasible, but here's the use case: I have
I'm not sure how else to describe this, outside of calling it a newspaper
I'm not sure how to describe this but I'm trying to create a base
I'm not sure this question is appropriate here but I hope I could get
I'm sure this is a fairly trivial problem, but I'm not sure what to
Not sure quite how to describe this but this is what i want to
Hmm.. I am not sure how to describe this. But I will try So
Not sure how else to explain this, so the title pretty much describes the

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.