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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:25:58+00:00 2026-05-28T00:25:58+00:00

I want to know how to replace some text in php string with like

  • 0

I want to know how to replace some text in php string with like an include or something.

here is my example string: I have a string “bla bla bla bla {{ module:projects,id=1 }} bla bla bla bla”

I want to find {{ module:projects,id=1 }} and make it include(‘projects.php?id=1’); right where it is in the string.

how would i pull this off?

An alternative if i could do this but i cant get it to execute the php from the string would be “bla bla bla bla bla bla”

  • 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-28T00:25:59+00:00Added an answer on May 28, 2026 at 12:25 am

    with preg_replace:

    $string = '{{ module:projects,id=1 }}';
    $string = preg_replace('/{{ module:([^,]+),id=([0-9]+) }}/', 'include(\'$1.php?id=$2\')', $string);
    // $string == include('projects.php?id=1')
    

    Note: you can’t actually include files with query strings on the end. PHP looks for file named ‘projects.php?id=1’.

    To get the file and actually include it:

    preg_match_all('/{{ module:([^,]+),id=([0-9]+) }}/', $string, $matches);
    foreach ($matches[1] as $key => $filename) {
        // set $id before including the file. you could optionally set $_GET['id'].
        $id = $matches[2][$key];
        include($filename . '.php');
    }
    

    To support multiple parameters. Similar to Matmarbon’s, but is more compact, doesn’t assume parameter values will conform to \w, doesn’t escape values, but also has less room for variations in spacing (/s*).

    preg_match_all('/{{ module:([^,]+)((,[^=]+=[^,}]+)*) }}/', $string, $matches);
    foreach ($matches[1] as $key => $filename) {
        foreach (explode(',', trim($matches[2][$key], ', ')) as $vars) {
            list($k, $v) = explode('=', $vars);
            $_GET[$k] = $v;
        }
        // then this:
        include($filename . '.php');
        // or, to replace content:
        ob_start();
        include($filename . '.php');
        $contents = ob_get_contents();
        ob_end_clean();
        $string = str_replace($matches[0][$key], $contents, $string);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: I have some text, like this php code if(empty($condition)) { // work here...
I have some text, something like this: Paragraphs of text (SOME KNOWN TEXT)Unknown Text(SOME
want to know why String behaves like value type while using ==. String s1
I have a huge text file, size > 4GB and I want to replace
I often have text in columns and need to replace some things without clobbering
I am trying to replace some text after an underscore in a filename. Example
Just a quick regex question...hopefully I have a string that looks something like this:
I want to use a technique like text-indent:-9999em or negative margin to replace my
I want to know what are the options to do some scripting jobs in
<textarea rows=18 cols=80 style=resize:none;> <?php $str = str_replace('<br>', '\n', 'some text<br><br>another line of text');

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.