Dear Friends,

I need to find and replace string within a text with a php include function.

For example let say my text is as below and the string i want to find and replace is register :

$text = "Heloo, This text is a simply created dummy text for testing. <br /> In this sentence we have a special word register to be replace with the php include word. Let's see whether it works....<br /><br />";

I want my function to find for the word "register " and replace it with php include function to include the registration_form.php..

My result should be echoing out the $text value. But of course we can't echo a funtion. So how to replace the "code" $text with the include function and display out the result?

I really need your help guys... Thank you...

    Do you actually need to include() the form file (i.e.: does it need to process any PHP code within it at that point?), or can you just grab the contents of the file with something like file_get_contents()?

      Hi,

      Yeah I need to include the file...

      I can't use the file_get_content function as i have some php script on the php file i want to include.

      Actually the purpose of me asking for this function is I want to create my own special character or something like tag. Where when that tag is found it will be converted into a function. For example when the word register is found I want it to include the registration_form.php file.. (This is just for an example)

      If anyone is familiar with Joomla, I trying to do something like the jumi... But just a smaller portion of it...

      Thanks for your responds..

        At the moment I'm more concern with the include function.... i want my special character to include a php file...

          Well, if I were doing it, I'd probably redefine the include file to actually define a function that returns the text, and which accepts a function parameter with the string you want to replace, and let it do the replacing. But, if that's not practical for now, you could use the ouput buffering functions.

          function foo($text, $keyword)
          {
             ob_start();
             include('the/include/file.php');
             $includeText = ob_end_clean();
             return preg_replace('/\b'.preg_escape($keyword).'\b/', $includeText, $text);
          }
          

            Hi NogDog,
            Thanks for your response...

            NogDog;10964575 wrote:

            Well, if I were doing it, I'd probably redefine the include file to actually define a function that returns the text, and which accepts a function parameter with the string you want to replace, and let it do the replacing. But, if that's not practical for now, you could use the ouput buffering functions.

            function foo($text, $keyword)
            {
               ob_start();
               include('the/include/file.php');
               $includeText = ob_end_clean();
               return preg_replace('/\b'.preg_escape($keyword).'\b/', $includeText, $text);
            }
            

            Can I know what the purpose of using '/\b' and '\b/' . Sorry not familiar with the usage. Then, Does this preg_escape function is still under use?

              Hi,

              I have coded it for testing as below :
              <table>
              <tr>
              <td>
              function code_include($text, $keyword, $file)
              {
              echo "<br />Hello from function<br />";
              ob_start();
              include($file);
              $includeText = ob_end_clean();
              return preg_replace($keyword, $includeText, $text);

              }

              $string_jumi = "Heloo, I just want to test whether the include can works inside the text. <br />
              For example let say we have this {JUMI} . and see what it returns <br /><br />";

              $regex = '/{(jumi)\s(.?)}/i';

              $registration = $_SERVER['DOCUMENT_ROOT']."/code/registration.php;

              code_include($string_jumi, $regex, $registration);
              </td>
              </tr>
              </table>

              but the result I get is just the

              Hello from function being displayed. The file is not included. Where I got it wrong?

              Thank you for the responses...

                {Added [noparse]

                ...

                [/noparse] tags around code. Please use them in future posts here. -- MOD}[/i][/COLOR]

                I have coded it for testing as below :

                function code_include($text, $keyword, $file)
                {
                echo "<br />Hello from function<br />";
                   ob_start();
                   include($file);
                   $includeText = ob_end_clean();
                   return preg_replace($keyword, $includeText, $text);
                
                }
                
                
                $string_jumi = "Heloo, I just want to test whether the include can works inside the text. <br />
                           For example let say we have this  {JUMI} . and see what it returns <br /><br />";
                
                $regex = '/{(jumi)\s*(.*?)}/i';
                
                
                $registration = $_SERVER['DOCUMENT_ROOT']."/code/registration.php;
                
                code_include($string_jumi, $regex, $registration);
                

                but the result I get is just the

                Hello from function being displayed. The file is not included. Where I got it wrong?

                Thank you for the responses...

                  Hi,
                  And I can't use echo code_include($string_jumi, $regex, $registration); as it will return 1 for include($file).

                  But I want it to return the text along with the included file and with the trailing text after the included file...

                  Can anyone let me know how to do this ??

                    vashini11;10964577 wrote:

                    Hi NogDog,
                    Thanks for your response...

                    Can I know what the purpose of using '/\b' and '\b/' . Sorry not familiar with the usage. Then, Does this preg_escape function is still under use?

                    The "\b" is a "word boundary". You may not need it if you're using a very unique place-holder text, but if you want to match on a complete word, then it may help avoid matching on something like "replace" while not matching on "irreplaceable".

                      I think you're pretty close. I made a couple changes to try, with comments:

                      <?php
                      function code_include($text, $keyword, $file)
                      {
                         echo "<br />Hello from function<br />";
                         ob_start();
                         include($file);
                         $includeText = ob_end_clean();
                         return preg_replace($keyword, $includeText, $text);
                      }
                      $string_jumi = "Heloo, I just want to test whether the include can works inside the text. <br />
                                 For example let say we have this  {JUMI} . and see what it returns <br /><br />";
                      // I think you need to escape the curly braces:
                      $regex = '/\{(jumi)\s*(.*?)\}/i';
                      $registration = $_SERVER['DOCUMENT_ROOT']."/code/registration.php;"
                      // need to echo the result (or assign it to a variable):
                      echo code_include($string_jumi, $regex, $registration); 
                      

                        Dear NogDog,

                        Thanks alot for your replies and Help...

                        I have code it as below :

                        function jumi_code($regex, $string){

                        if(preg_match($regex, $string)== 1){
                        
                            $string_jumi_exploded = explode("{", $string);
                            echo $string_jumi_exploded[0]."<br />";
                            $string_jumi_exploded_deep = explode("}", $string_jumi_exploded[1]);
                        
                            $string_to_explode = $string_jumi_exploded_deep[0];
                        
                            //TO GET THE FILE NAME
                                $string_exploded = explode("|",$string_to_explode);
                                $array = explode("-",$string_exploded[1]);
                                $registration = $_SERVER['DOCUMENT_ROOT']."/".$array[0]."/".$array[1]."/code/".$array[2];
                            //END
                        
                           //INCLUDE THE FILE
                                if (file_exists($registration)) {
                                    include($registration);
                                } else {
                                    echo "The file <b style=\"color: #990000;\">$array[2]</b> does not exist <br />";
                                }
                        
                            echo $string_jumi_exploded_deep[1];
                        }
                        else{
                            echo $string;
                        }
                        
                        echo "<hr>";

                        }

                        $string_jumi = "Heloo, I just want to test whether the include can works inside the text. <br />
                        For example let say we have this {JUMI|15-14-registration.php|} . and see what it returns.<br /><br /> ";

                        $regex = '/{(jumi)\s(.?)}/i';

                        jumi_code($regex,$string_jumi);

                        This didn't really not the good solution for my problem... But this the solution I'm using for the moment....

                        Thanks alot for being the helping hand friend.. Really appreciated it...

                          Write a Reply...