Formula - Replace (v6+)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • esforim
    Active Community Member
    • Jan 2020
    • 2204

    Formula - Replace (v6+)

    https://docs.espocrm.com/user-guide/...sions/#replace

    (Please note, I mistakenly used Complex Expression instead of Formula, however there is a solution for both method thanks to item and Maximus)

    I'm trying to use the replace formula and having difficult with it, anyone know how to use it?

    Here is my formula but not getting it to work.

    Code:
    filename = string\concatenate(iD, ' - ', name);
    filename = replace('test0', 'test1', 'test2');
    Seeing that it didn't work, I decide to just comment out the first line to see if my Replace formula work at all. Still no luck so I also tried to just have 2 string. But still nothing, anyone know how to use this formula? It a new feature from v6.

    Code:
    /* filename = string\concatenate(iD, ' - ', name); */
    filename = replace('test0', 'test1');
    I'm planning to replace all non-compatible character filename, for example these type of characters \ / : ; ? change to _
    Last edited by esforim; 11-25-2020, 06:23 AM.
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #2
    Hello,
    i can try it ... but need more info :
    i use this :
    $name = $entity->get('xmlNumber').'-' .\Espo\Core\Utils\Util::sanitizeFileName($entity->get('name')). '.pdf';

    so special char are disabled...
    maybe i can make a special formula for you

    just one requirement : the function name must be name : replaceByItem( field ) (joke)
    Regards

    $newPathToFile = preg_replace('/\s+/', '_', $pathToFile);
    Last edited by item; 11-24-2020, 01:18 AM.
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment

    • item
      Active Community Member
      • Mar 2017
      • 1476

      #3
      custom/Espo/Custom/Core/Formula/Functions/StringGroup/PregReplace.php

      PHP Code:
      <?php
      /************************************************** **********************
      * This file is part of EspoCRM.
      *
      * EspoCRM - Open Source CRM application.
      * Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
      * Website: http://www.espocrm.com
      *
      * EspoCRM is free software: you can redistribute it and/or modify
      * it under the terms of the GNU General Public License as published by
      * the Free Software Foundation, either version 3 of the License, or
      * (at your option) any later version.
      *
      * EspoCRM is distributed in the hope that it will be useful,
      * but WITHOUT ANY WARRANTY; without even the implied warranty of
      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      * GNU General Public License for more details.
      *
      * You should have received a copy of the GNU General Public License
      * along with EspoCRM. If not, see http://www.gnu.org/licenses/.
      *
      * The interactive user interfaces in modified source and object code versions
      * of this program must display Appropriate Legal Notices, as required under
      * Section 5 of the GNU General Public License version 3.
      *
      * In accordance with Section 7(b) of the GNU General Public License version 3,
      * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
      ************************************************** **********************/
      
      namespace Espo\Custom\Core\Formula\Functions\StringGroup;
      
      use \Espo\Core\Exceptions\Error;
      
      class PregReplace extends \Espo\Core\Formula\Functions\Base
      {
      public function process(\StdClass $item)
      {
      if (!property_exists($item, 'value')) {
      throw new Error();
      }
      
      if (!is_array($item->value)) {
      throw new Error();
      }
      
      if (count($item->value) < 2) {
      throw new Error();
      }
      
      $pattern = $this->evaluate($item->value[0]);
      $replacement = $this->evaluate($item->value[1]);
      $string = $this->evaluate($item->value[2]);
      // "/[^0-9]/"
      return preg_replace( $pattern , $replacement, $string);
      }
      }
      Last edited by item; 11-24-2020, 01:31 AM.
      If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

      Comment

      • item
        Active Community Member
        • Mar 2017
        • 1476

        #4
        custom/Espo/Custom/Ressources/app/formula.json
        PHP Code:
        {
        "functionList": [
        "__APPEND__",
        {
        "name": "string\\pregReplace",
        "insertText": "string\\pregReplace(PATTERN, REPLACEMENT, STRING)"
        }
        ],
        "functionClassNameMap": {
        "string\\pregReplace": "\\Espo\\Custom\\Core\\Formula\\Functions\\StringGroup\\PregReplace"
        }
        } 
        

        so use in formula blabla = string\pregReplace( .... )
        Last edited by item; 11-24-2020, 01:34 AM.
        If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

        Comment

        • Maximus
          Senior Member
          • Nov 2018
          • 2731

          #5
          Hello folks,
          I just want to add my 5 cents here. esforim the link you provided (https://docs.espocrm.com/user-guide/...sions/#replace) is not about Formula. It applies to Complex expressions. So the formula: replace('test0', 'test1', 'test2'); is wrong.
          The appropriate formula is located here https://docs.espocrm.com/administrat...#stringreplace. You can utilize the regEx with it. See regEx tips here https://docs.espocrm.com/administrat...a/#stringmatch.
          Last edited by Maximus; 11-24-2020, 09:05 AM.

          Comment

          • esforim
            Active Community Member
            • Jan 2020
            • 2204

            #6
            Wow how silly of me! Thank Maximus. But look like item just created a great tutorial anyhow.

            I will give stringreplace a try. And link the thread on the 'wiki' for future user for Complex Expression
            Last edited by esforim; 11-25-2020, 06:17 AM.

            Comment

            • esforim
              Active Community Member
              • Jan 2020
              • 2204

              #7
              Hi guys,

              I just tried using this formula and having trouble with it still. Not sure how to use it. Tried variation of it (many of which isn't copy/paste) but never got the formula to work, here is some of the formula I tried.

              Code:
              name = string\replace('Hello {description}, '{description}, 'world)
              string\replace('Hello {description}, '{description}, 'world)
              string\replace('Hello {name}, '{name}, 'world)
              string\replace("Hello {name}", {name}, "world")
              string\replace("Hello", world, {name})
              And my field value:
              Code:
              World 1 Hello World 1 1 World Hello {name} World {name}
              Basically I just want to able to Replace all "Hello" to "World" in the {name} field. Anyone can point me in the right direction? The example code in documentations is difficult to understand as it is not a live working example.
              Last edited by esforim; 01-11-2021, 11:41 PM.

              Comment

              • Maximus
                Senior Member
                • Nov 2018
                • 2731

                #8
                Hi,
                This works like a charm:
                Code:
                name = string\replace('Hello {test}', '{test}', 'world');

                Comment

                • esforim
                  Active Community Member
                  • Jan 2020
                  • 2204

                  #9
                  Thank Maximus, it does seem to work, it is because it is missing the ending ' symbol? Should documents be update to include those.

                  However the issue is that using this formula it is replace everything and delete all the other words.

                  For example:

                  Hello Hello 123 > Formula > Hello World

                  How would I make the formula like this?
                  Hello Hello 123 > Formula > World World 123

                  Or is it not possible with the current formula system?

                  And what is {test} field?
                  Last edited by esforim; 01-12-2021, 11:25 PM.

                  Comment

                • Maximus
                  Senior Member
                  • Nov 2018
                  • 2731

                  #10
                  For Hello Hello 123 > Formula > World World 123 try this:
                  Code:
                  name = string\replace('Hello {123} 123', '{123}', 'World');
                  {tests} and {123} it is just like a variable for the second element. You can put any value within {} that you want.

                  Comment

                  • esforim
                    Active Community Member
                    • Jan 2020
                    • 2204

                    #11
                    Finally success! Thank you for your help guys. Finally got it to work based on those formula.

                    Some learning can be found here of my trial can be found here: https://forum.espocrm.com/forum/gene...6269#post66269

                    If you just want the code; this is the final code I used to replace all "Hello" with the "World" without affecting what is inputted in the field: name

                    Code:
                    name = string\replace(name, 'Hello', 'World');
                    Hello Testing the Formula Hello Again > (Formula) > World Testing the Formula World Again
                    Last edited by esforim; 01-14-2021, 01:05 AM.

                    Comment


                    • item
                      item commented
                      Editing a comment
                      Greats ... happy for you.. need help.. don't hesitate to post
                  • esforim
                    Active Community Member
                    • Jan 2020
                    • 2204

                    #12
                    Nothing much, but here is my formula to replace the address to a more acceptable format for Windows PC. This is use for Real Estate extensions where I replace the text "/" with "_", you can use this for Contact and Account as well or anywhere that have address field.

                    The reason I need this because / is usually use for Unit address in my country which doesn't play well with Windows PC. Using this formula string will replace the / with _. You may have to change your field name slightly for it to work fully for you.

                    Code:
                    ifThenElse(
                    addressStreet,
                    folderNameProp = string\concatenate(addressStreet, ", ", addressCity, " ", addressState, " ", addressPostalCode," (", propertyREID, ")"),
                    folderNameProp = string\concatenate(addressCity, " ", addressState, " ", addressPostalCode," (", propertyREID, ")")
                    );
                    folderNameProp = string\replace(folderNameProp, '/', '_');
                    folderNameProp is field name I want to concatenate the address
                    I use ifThenElse because sometime I don't have street name, only city name (Place of Birth)
                    propertyREID is my custom field which auto-increment so I can give each address a numbers for importing/exporting or identificaiton

                    finally I string replace the / (slash) to _ (underscore).

                    For example input of the address:

                    Input: Unit 1/2 Streetname, City STATE Postcode
                    Output: Unit 1_2 Streetname, City STATE Postcode
                    Last edited by esforim; 03-01-2021, 07:30 AM.

                    Comment

                    Working...