String\replace for newlines

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tziere
    Member
    • Jul 2020
    • 42

    String\replace for newlines

    Hi All,

    I was wondering if we could use the string\replace function to also replace newlines. If yes, how would that work?

    To illustrate, I want change:

    "hello
    world"

    into

    "hello \n world"

    Thanks in advance!
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #2
    try


    $str = str_replace(array("\r","\n"),"",$str);

    else need new formula
    Is there a PHP string function that transforms a multi-line string into a single-line string? I'm getting some data back from an API that contains multiple lines. For example: <p>Some Data&...


    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment

    • Tziere
      Member
      • Jul 2020
      • 42

      #3
      Thanks for the inspiration.

      If anyone is interested, I managed it by doing the following:

      - Creating a varchar field, called connector, with default value: \n
      - then formula: newBody = string\replace(body,'\n',connector);

      Comment

      • telecastg
        Active Community Member
        • Jun 2018
        • 907

        #4
        Thanks for posting your solution

        Comment

        • item
          Active Community Member
          • Mar 2017
          • 1476

          #5
          Hello,
          in the past.. i was do this..
          maybe to incorporate to extention of formula of emillod
          it's a little more complexe for make "patern".. but you can do what you will with

          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);
          }
          }
          If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

          Comment

          • emillod
            Active Community Member
            • Apr 2017
            • 1405

            #6
            Hi guys, i thought formula for replacement is already there. I can check that and if no, then i'll add this to extension, no problem

            Comment

            • emillod
              Active Community Member
              • Apr 2017
              • 1405

              #7
              It's look like similar formula is already there: https://github.com/espocrm/espocrm/b...eplaceType.php

              Comment

              • item
                Active Community Member
                • Mar 2017
                • 1476

                #8
                Hello emillod, it's not really similar.. because imagine how you only will digit in a string ?
                or you only need ABCD.... in a string ?
                you can search in espoCRM how many time preg_replace is used
                i think is more "developper" than user capacity and it's why Yuri don't make this out-of-box

                sample : patern ""/[^0-9]/""
                result = only digit

                Best regards
                Last edited by item; 09-08-2021, 09:50 PM.
                If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

                Comment

                • emillod
                  Active Community Member
                  • Apr 2017
                  • 1405

                  #9
                  Okay. Let me go through this, i'll try to add this to our extension item do you have any ideas for other formulas? If i'm adding one, i could also add anothers

                  Comment


                  • item
                    item commented
                    Editing a comment
                    Hello emillod 
                    no i have no idea.. 
                    i have some formula but only specific to belgium : 
                    check if valid belgium identity card number
                    birthday form identity card number


                    I think your next goal must be : 
                    how make this kind of function :

                    entity\SaveParent(entityType, entityId );  => for sample, recalculate calculetField in parent.
                    i don't know if it's the good "name of function"..
                    but this, i imagine so : 
                    if i remember formula is "beforeSave" : 
                    so we need to run the function in afterSave with some think like a global hook : afterSave .. 
                    maybe it's not possible (actually), i think if possible, Yuri make this since longtime
                Working...