Welcome Guest, Not a member yet? Register   Sign In
Parsing page content
#1

[eluser]jay2003[/eluser]
Hi,

I am building a CMS and have created helpers for certain items such as forms, image sliders, photo gallery etc.

I am now integrating them with my page loader controller and am a little stuck, I have added dropdowns to tinymce which insert the code ##FORMBUILDER-1## or ##GALLERY-2## etc where the number is the unique key for the form/gallery they have selected.

I have used the following code before but only for a single contact form, for this system I now need to be able to retrieve the number after the - automatically so I can pass it into my helper to retrieve the correct form.

Code:
$this->load->library('parser');    
$this->parser->set_delimiters('##', '##');
$content = $page['page_content'];
$replacements = array(
   '##FORMBUILDER-1##' => form_builder(1)
);
  
$new_page_content = $this->parser->parse_string($content, $replacements, TRUE);

I know I could do it by creating this replacements array for all of my forms, galleries etc that are on the system and then it would just replace the ones which are included in the page but feel thats a little wasteful for each page load of the system.

Is there a way to get this value from the text so I can just create the replacements array with the required values for each page?

Thanks in advance

Jason
#2

[eluser]CroNiX[/eluser]
Sure, just strip all non-numeric characters from the string.

Code:
$str = '##FORMBUILDER-1##';
$removed = preg_replace('/\D/', '', $str);  
echo $removed;  // echos 1
#3

[eluser]jay2003[/eluser]
Thanks - I needed to retrieve these values from the page content as there may or may not have been forms or photo galleries so I came up with this.

Is this an acceptable solution? Its working anyway...

Code:
$no_of_matches = preg_match_all('/##(.*?)##/', $data['site_page']['page_content_1'], $matches);
  
   if ($no_of_matches>0)
   {
    $content = $data['site_page']['page_content_1'];
    
    $this->load->library('parser');
    $this->parser->set_delimiters('##', '##');
    
    foreach ($matches[1] as $match)
    {
     $replace_me =  explode('-',$match);
    
     if ($replace_me[0]=='PHOTOGALLERY')
     {
      $final_replace = build_gallery($replace_me[1]);
     }
     if ($replace_me[0]=='FORMBUILDER')
     {
      $final_replace = build_form($replace_me[1]);
     }
     $replacements[$replace_me[0].'-'.$replace_me[1]] = $final_replace;
    
      };
  
    $data['site_page']['page_content_1'] = $this->parser->parse_string($content, $replacements, TRUE);
   }

Thanks

Jason




Theme © iAndrew 2016 - Forum software by © MyBB