Announcement

Collapse
No announcement yet.

String replace in formula

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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!';

  • #2
    No. You need to write a custom function: https://github.com/espocrm/documenta...-in-formula.md.

    Comment


    • #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:
      Last edited by shef; 03-20-2020, 11:21 AM.

      Comment


      • #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
          item commented
          Editing a comment
          it's wrong :

          string\replace('Hello {test}, '{test}, 'world) .. must be :

          string\replace('Hello {test}', '{test}', 'espocrm, will learn formula');

          result :

          Hello espocrm, will learn formula

          php => https://www.php.net/manual/fr/function.str-replace.php

      • #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(addressStreetaddressPostalCodeaddressCityaddressState);
        lon=string\Longitude(addressStreetaddressPostalCodeaddressCityaddressState);
        inami=string\substring(inamiNumber08); 
        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.

        Comment


        • #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);
          }

          Comment

          Working...
          X