String replace in formula

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shef
    Member
    • Jul 2018
    • 38

    String replace in formula

    Is it possible to replace a substring in a string using formula?

    Code:
    $str = 'Hello world!';
    
    // how would I replace the word
    // "world" with "there" so I can have this
    
    $str = 'Hello there!';
  • yuri
    Member
    • Mar 2014
    • 8444

    #2
    No. You need to write a custom function: https://github.com/espocrm/documenta...-in-formula.md.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • shef
      Member
      • Jul 2018
      • 38

      #3
      Thanks, Yuri. For everyone that wants to use it, you can find it below.

      Create two files below and Clear cache.

      Code:
      custom/Espo/Custom/Core/Formula/Functions/StringGroup/ReplaceType.php
      PHP Code:
      <?php
      namespace Espo\Custom\Core\Formula\Functions\StringGroup;
      
      use Espo\Core\Exceptions\Error;
      
      class ReplaceType extends \Espo\Core\Formula\Functions\Base
      {
          public function process(\StdClass $item)
          {
              $args = $this->fetchArguments($item);
      
              if (count($args) < 3) {
                  throw new Error('Replace: Too little argunents.');
              }
      
              $search = $args[0];
              $replace = $args[1];
              $subject = $args[2];
      
              if (!is_string($subject) || !is_array($subject)) {
                  $subject = strval($subject);
              }
      
              return str_replace($search, $replace, $subject);
          }
      }
      }
      Code:
      custom/Espo/Custom/Resources/metadata/app/formula.json
      Code:
      {
          "functionList": [
              "__APPEND__",
              {
                  "name": "string\\replace",
                  "insertText": "string\\replace(SEARCH, REPLACE, SUBJECT)"
              }
          ],
          "functionClassNameMap": {
              "string\\replace": "\\Espo\\Custom\\Core\\Formula\\Functions\\StringGroup\\ReplaceType"
          }
      }

      Usage
      Code:
      $str = 'Hello world!';
      $str = string\replace('world', 'there', $str); // Hello there!
      You can use array for search and replace arguments.
      Code:
      $str = 'Hello world!';
      $str = string\replace(list('Hello', 'world'), list('Hi', 'there'), $str); // Hi there!
      Note: The matching is case sensitive.

      Full underlying function reference:
      PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.
      Last edited by shef; 03-20-2020, 11:21 AM.

      Comment

      • esforim
        Active Community Member
        • Jan 2020
        • 2204

        #4
        Hi everyone,

        I try so many variation of formula but still having no luck at the moment, can anyone tell me where I went wrong. I'm not using the custom formula above as it seem too high level, just using the default formula provided in here:

        https://docs.espocrm.com/administrat...#stringreplace

        I tried variation of the example but no luck.

        string\replace('Hello {test}, '{test}, 'world)

        Question:
        1) Do I need to add the field at the field? For example:
        description = string\replace('Hello {test}, '{test}, 'world)

        2) what is {test}? Is that the field name I need to replace?
        string\replace('Hello {description}, '{description}, 'world)

        I'm quite confused with how to use this formula, anyone can write a working formula that I can use using the EspoCRM default field?

        For example
        string\replace('Hello {name}, '{name}, 'world)

        Would convert "Hello" to "Hello world" or something like this
        string\replace('Hello {name}, '{name}, 'world)

        Comment

      • item
        Active Community Member
        • Mar 2017
        • 1476

        #5
        Hello espocrm,


        PHP Code:
        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);
        }
        } 
        
        and sample :

        PHP Code:
        practrice=string\PregReplace('/[^0-9]/', '', practrice);
        firstName=string\trim(firstName);
        lastName=string\trim(lastName);
        birthday=string\Birthday(nationalNumber);
        lat=string\Latitude(addressStreet, addressPostalCode, addressCity, addressState);
        lon=string\Longitude(addressStreet, addressPostalCode, addressCity, addressState);
        inami=string\substring(inamiNumber, 0, 8); 
        
        formula give us just a way to use "php function" .. see https://www.php.net/manual/fr/function.preg-replace.php
        open new thread about formula ... all request .. on comment for the 1er post ..and we try to post new formula
        my formula don't respect camelCase.. i just see
        Last edited by item; 07-08-2020, 09:20 PM.
        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

          #6
          Birthday from belgium national number
          PHP Code:
          namespace Espo\Custom\Core\Formula\Functions\StringGroup;
          
          use \Espo\Core\Exceptions\Error;
          
          class Birthday extends \Espo\Core\Formula\Functions\Base
          {
          public function process(\StdClass $item)
          {
          if (!property_exists($item, 'value')) {
          throw new Error();
          }
          
          if (!$item->value[0]) {
          throw new Error();
          }
          
          return substr ( $this->evaluate($item->value[0]), 2, 4 );
          }
          } 
          
          If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

          Comment

          Working...