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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:09:09+00:00 2026-06-15T23:09:09+00:00

I am trying to replace few words from a huge string with preg_replace() using

  • 0

I am trying to replace few words from a huge string with preg_replace()
using it like this :

preg_replace($match[0], $variable_value_array, $form_body)

Here $match[0] is an array with value in it in the form like:

$contacts-firstname$
$contacts-lastname$
$contacts-mobile$
$leads-leadstatus$
$leads-noofemployees$

and $variable_value_array is also an array with values in it like :

Linda
William
(091) 115-9385
Value Not Present
Value Not Present

and $form_body is a really long string.

The function is replacing the values of $form_body but instead of replacing the whole $contacts-firstname$ with Linda it is replacing only contacts-firstname with Linda making it like $Linda$. What should i do to replace both the $ sigh’s as well ?
Thanks.

  • 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-15T23:09:10+00:00Added an answer on June 15, 2026 at 11:09 pm

    That’s because $ is interpreted as the delimiter of your regex. You should use a simple str_replace instead. The signature is exactly the same:

    str_replace($match[0], $variable_value_array, $form_body);
    

    If you desperately wanted to use preg_replace you need to do two things. Firstly, you need to wrap every array element in explicit delimiters (/ is kind of the standard choice). And also you need to run every array element through preg_quote, otherwise the $ will be treated as an end-of-string anchors:

    $patterns = array();
    foreach($match[0] as $value)
        $patterns[] = '/'.preg_quote($value, '/').'/';
    

    And then use $patterns instead of $match[0]. But that is only intended as a nice-to-know if you ever actually need to use an array of literal search-strings inside a more complex pattern.

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

Sidebar

Related Questions

I am trying to replace occurances of a string in a few different steps,
I am trying to replace a value with a passed value using javascript or
I am trying to replace a large string in groovy. But can't get it
I am trying to replace a comma in a string. For example, the data
I just spent a few hours pulling my hair out over this. I'm trying
I was trying to replace this (' with (\' and ') with \') My
just trying to tie up a few loose odds and ends here. I have
I am trying to replace properties on a properties file using sed in a
I am using RJS with Prototype and I am trying to replace HTML code
I am trying to substitute few special characters using sed/perl for which I am

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.