1 2 Previous Next 17 Replies Latest reply on Nov 12, 2014 2:17 AM by steve.winter

    Extracting paragraphs from a field with CWP and PHP

    pewi

      Hi,


      I am building af website, where I want to extract contents from a text field using PHP. In the FM application the field contains several paragraphs each seperated by a carriage return. I need to translate the whole contents into html wrapping each paragraph in a <p> element instead of the carriage return. But how do I accomplice something like that? Do I need to run a script in FM or is there a simpler way?

       

      I would appreciate some good advice, if someone out there know of a solution.

       

      Regards

       

      Peter

        • 1. Re: Extracting paragraphs from a field with CWP and PHP
          beverly

          "

          " & substitute(field; "¶"; "

          " ) & "

          "
          The starts the para, changes every return with a close and open, finally closing again. If you wind up with "

           

          " at the end, you can leave or remove those, too.

           

           

           

          Beverly

          • 2. Re: Extracting paragraphs from a field with CWP and PHP
            pewi

            Thank you, Beverly, but I'm not quite following you.

            • 3. Re: Extracting paragraphs from a field with CWP and PHP
              Mike_Mitchell

              Peter -

               

              This is a calculation I use to clean up the tags and carriage returns on my web site. Set it as an auto-enter calc, replacing the value:

               

              -------------------------------------------

               

              TextFormatRemove (

               

              Case ( Left ( Self ; 3 ) ≠ "<p>" ; "<p>" ) &

               

              Substitute ( Self ;

               

              [ "</p></p>" ; "</p>" ] ;

              [ "<p><p>" ; "<p>" ] ;

              [ "<br /><br />" ; "<br />" ] ;

              [ "¶¶" ; "</p>¶¶<p>" ] ;

              [ "¶" ; "<br />¶" ] ;

              [ "<br />¶<br />¶" ; "¶¶" ] ;

              [ "<br /><br />" ; "<br />" ] ;

              [ "<p></p>" ; "" ] ;

              [ "</p></p>" ; "</p>" ] ;

              [ "<p><p>" ; "<p>" ] )

               

              ) &

               

              Case ( Right ( Self ; 4 ) ≠ "</p>" ; "</p>" )

               

               

              HTH

               

              Mike

              • 4. Re: Extracting paragraphs from a field with CWP and PHP
                pewi

                I'm still a bit mystified. Is it a calculation that you run in FM or is it a function within your php-script?

                 

                I was wondering if I could use the php function str_replace("","</p><p>" ,$var ) but somehow this doesn't find the carriage returns in the string. I also tried with "&para", no luck.

                • 5. Re: Extracting paragraphs from a field with CWP and PHP
                  Mike_Mitchell

                  It is a calculation in the field definitions on the FileMaker side. Go to File > Manage > Database and choose the Auto-Enter tab. Be sure to  un-check the "Do not replace" checkbox.

                   

                  You can do it on the PHP side, but not using this syntax.

                  • 6. Re: Extracting paragraphs from a field with CWP and PHP
                    JoelShapiro

                    Hi Peter

                     

                    Are you aware of PHP's nl2br function?

                     

                    If you do something like:

                     

                    echo nl2br( $record->getField('field-with-returns') );

                     

                    you'll get your FM field with <br>s separating each paragraph -- so it will *look* like the separate paragraphs you see in FMPro.  Then if you really want to change those into separate <p> elements, you should be able to use str_replace like you suggested (but replacing "<br>" or "<br/>")

                     

                    HTH,

                    -Joel

                    1 of 1 people found this helpful
                    • 7. Re: Extracting paragraphs from a field with CWP and PHP
                      LyndsayHowarth

                      Wow! You broke FMIs code, Mike! LOL

                       

                      Pewi, you can also do this in the PHP. PHP has a corresponding set of string functions to those found in FileMaker.

                       

                      Sent from my iPad

                      Lyndsay Howarth

                      11th Hour Group Pty Ltd

                      • 8. Re: Extracting paragraphs from a field with CWP and PHP
                        Mike_Mitchell

                        Based on your original statement, you wanted to extract the text from a FileMaker field and replace carriage returns with "<p>" tags. Is that not the problem you're trying to solve? What's the driver for wanting to do it PHP-side?

                         

                        If you do it when the data are stored, you never have to mess with it again. Why do the work of processing the text every time you fetch it? I'm missing something.

                        • 9. Re: Extracting paragraphs from a field with CWP and PHP
                          Mike_Mitchell

                          Of course, if you're sure you want to do this when you fetch the data, here's a function I use. It's a little more thorough than the standard nl2br Joel mentioned (I, like you, tend to like paragraph tags rather than <br /> tags):

                           

                          function para_tag_string($str) {

                           

                          // Format string to allow for user-input carriage returns

                           

                                    $str = preg_replace('[\r\n]', '</p><p>', $str);

                                    $str = preg_replace('[<p><ul></p>]', '<ul>', $str);

                                    $str = preg_replace('[<p></ul></p>]', '</ul>', $str);

                                    $str = preg_replace('[<p><ol></p>]', '<ol>', $str);

                                    $str = preg_replace('[<p></ol></p>]', '</ol>', $str);

                                    $str = preg_replace('[<p><li>]', '<li>', $str);

                                    $str = preg_replace('[</li></p>]', '</li>', $str);

                                    $str = preg_replace('[<p></p>]', '', $str);

                                    if(strpos($str, '<p>') !== 0 and strpos($str, '</p>') !== FALSE ) {

                                              $str = '<p>'.$str.'</p>';

                                    }

                           

                                    return $str;

                           

                          }

                           

                          This cleans up some issues with ordered and unordered lists (they tend to get all cattywumpus when you just do a raw preg_replace). Between this and the FileMaker calc, one of them should suit.

                           

                          Mike

                          • 10. Re: Extracting paragraphs from a field with CWP and PHP
                            user19752

                            CR in field is LF in PHP API, so

                            str_replace("\n","</p><p>" ,$var)

                            • 11. Re: Extracting paragraphs from a field with CWP and PHP
                              LyndsayHowarth

                              It is... which is why the nl2br puts in a <br> not a <p>

                               

                              \r is CR ... ie

                               

                              If you enter the values from the PHP or use checkboxes it is likely they are \n new lines .... but if you manually enter a CR in a field in FM it is a CR ... ie

                               

                              Interestingly I was passing such calculations between v12 and 13 yesterday and I was getting slightly different results which I haven't had time to analyse yet.

                               

                              - Lyndsay

                               

                              PS I made this bigger cause I am getting sick of squinting.... I need a new script for my spec's.

                              • 12. Re: Extracting paragraphs from a field with CWP and PHP
                                steve.winter

                                As an aside Mike I presume you’re aware that you can pass arrays into reg_replace…?

                                 

                                $str = preg_replace(

                                     array(‘thing 1’, ‘thing 2’, ‘thing 3’),

                                     array(‘replace 1’, ‘replace 2’, ‘replace 3’),

                                     $str

                                );

                                 

                                Cheers

                                Steve

                                • 13. Re: Extracting paragraphs from a field with CWP and PHP
                                  user19752

                                  I tried to write most short answer for pewi, that using ¶ for CR is allowed only in FM calculation, php get it as is (a visible character).

                                   

                                  But I tested only on FM13S and hadn't test entering data from PHP.

                                  nl2br or regex for any combination of line breaks are better than only "\n".

                                   

                                  regards

                                  • 14. Re: Extracting paragraphs from a field with CWP and PHP
                                    steve.winter

                                    As an aside Mike I presume you’re aware that you can pass arrays into reg_replace…?

                                     

                                    $str = preg_replace(

                                         array(‘thing 1’, ‘thing 2’, ‘thing 3’),

                                         array(‘replace 1’, ‘replace 2’, ‘replace 3’),

                                         $str

                                    );

                                     

                                    Cheers

                                    Steve

                                    1 2 Previous Next