How to format a simple array of PHP strings?

I have this simple function that I pass in an array of strings:

function myfunction( $arg = array() )
{
    // do stuff to $arg...
    // return a $string;
}

Simple so far, but I need some lines in the array to $argbe formatted and some to remain unformatted . I can not figure out how to do this?

Let's say I ran this $argone through myfunction():

echo myfunction( array( 'format me!', 'do not format me!' ) );

My little brain cannot figure out how to report myfunction()that the first value in the array $argshould have formatting, and it should not format the second value.

I was thinking of an associative array, but I think this may be the wrong approach due to having the same indexes.

echo myfunction( array( 'format' => 'hi', 'format' => 'bye', 'noformat' => 'foo');

Just find the “push” in the right direction.

EDIT 1:

, $arg, .

2:

$arg , .

+3
5

:

function myfunction(array $format, array $noformat) {
  ... 
}

function myfunction(array $strings) {
  foreach ($strings['format'] as $format) {
    // do stuff
  }
  foreach ($strings['noformat'] as $noformat) {
    // do stuff
  }
}

:

myfunction(array(
  'format' => array('one', 'two', 'three'),
  'noformat' => array('four', 'five', 'six'),
));

( ) , :

$strings = array(
  'one' => true,
  'two' => false,
  'three' => false,
  'four' => true,
);

myfunction($strings);

:

function myfunction(array $strings) {
  foreach ($strings as $k => $v) {
    if ($v) {
      // format string
    }
  }
}

, , .

+4

, myfunction() , . array_map .

:

function myfunction($element) {
  if( do-formatting) {
    //do your formatting stuff
    $element = formatted-stuff
  }

  return $element
}

$arr = array("format me", "don't format me");

//This calls myfunction on each element of the array
//A new array is returned where each element has been replaced
//by the return value from myfunction.  
$arr = array_map('myfunction', $arr);
+1

. __tostring , myFunction.

<?php
class MyString{
    private $_value;
    public $to_format;

    public function __construct($str, $to_format = true){
        $this->_value = $str;
        $this->to_format = $to_format;
    }

    public function __tostring(){
        return $this->_value;
    }
}

$args = array( new MyString('format me'), new MyString('Not me!', false) );
+1

This is strangely similar to how table rows are formatted (with a different formatting function for each row). I would provide the data as a single array, and then provide the formatting information as a second key-based array. For instance:

function rowFormatter(Array $data, Array $format=array()) {
  ob_start();
  foreach ($data as $key => $value) {
    if (isset($format[$key])) 
      echo call_user_func($format[$key],$value);
    else 
      echo $value;
  }
  return ob_get_clean();
}

function makeBold($x) { return '<b>'.$x.'</b>'; }

rowFormatter(array( "Hello", "Mister" ), array( 0 => 'makeBold' ));
0
source

I do not know what these lines that you format are, so I used common names for these classes:

class LineItem
{
    var $value;

    function __construct($val)
    {
        $this->value = $val;
    }

    function __toString()
    {
        return $this->value;
    }

    function getFormatted( $formatter )
    {
        return $this->value;
    }
}

class FormattedLineItem extends LineItem
{
    function getFormatted( $formatter )
    {
        return $formatter( $this->value );
    }
}

function myfunction( $arr=array() )
{
    $fnFormat = function($str) {
        return ucwords($str);
    };

    foreach($arr as $obj)
    {
        $str = $obj->getFormatted($fnFormat);
        echo "$str\n";
    }
}

$arr = array(
    new FormattedLineItem('format me!'),
    new LineItem('do not format me!')
);

myfunction($arr);

The idea is to use separate classes to distinguish between strings.

0
source

Source: https://habr.com/ru/post/1727230/


All Articles