SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2008
    Location
    Brisbane, Australia
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    string replace with include file - how to?

    Hi All,

    I have a basic CMS that saves the data into a MySQL text field. My client wants within one of the pages a contact form so I somehow need to replace a keyword within the content string from the DB with an include file, below is what I have so far but it is just producing 1 each time.

    Any idea where I am going wrong?

    Thankyou

    PHP Code:
      // FUNCTION TO REPLACE 
      
    function code_include($text$keyword$file
      { 
         
    ob_start(); 
         include(
    $file); 
         echo 
    $file;
         
    $includeText ob_end_clean(); 
         return 
    str_replace($keyword$includeText$text); 
      } 
      
      
    // DEFINE STRING FROM DB
      
    $content_string stripslashes($content->description);
      
      
    // DEFINE KEYWORD 
      
    $thekeyword 'FORMINSERT'
      
      
    // DEFINE THE FORM
      
    $registration $_SERVER['DOCUMENT_ROOT']."/includes/inc_fusion.php"
      
      
    // ECHO THE RESULT OUT 
      
    echo code_include($content_string$thekeyword$registration); 

  2. #2
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    ob_start(); 
         include($file); 
         echo $file;
         $includeText = ob_get_contents();
         ob_end_clean();

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    You're calling ob_end_clean() which just ends the buffer, it returns TRUE which gives you the 1 when echoed.

    To end the buffer and retrieve its contents in one step, you can use ob_get_clean() (docs).
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •