Programmatically replace values' presentation in PHP array

Andres SK

I have these volume variable names. Before presenting them to the users, they need to be "retouched".

$unit[] = 'cm3';
$unit[] = 'barrel_petrolium';
$unit[] = 'register_tons';
$unit[] = 'ocean_tons';
$unit[] = 'gal_us';
$unit[] = 'gal_uk';
  1. 3 needs to be converted to ³
  2. _ needs to be converted to one space
  3. when there is a _xx at the end, it needs to be converted to (XX)

I have no problems for the first 2 rules. How can I also apply the 3rd rule?

# replacements
$search  = array('3', '_');
$replace = array('³', ' ');

# units
$temp = str_replace($search, $replace, $unit); //1st and 2nd rules

$formatted[] = $temp;
print_r($formatted);

The result would be:

cm³
barrel petrolium
register tons
ocean tons
gal us
gal uk

It should be:

cm³
barrel petrolium
register tons
ocean tons
gal (US)
gal (UK)

Any ideas? Thanks!

Casimir et Hippolyte

You can use preg_replace_callback:

$unit[] = 'cm3';
$unit[] = 'barrel_petrolium';
$unit[] = 'register_tons';
$unit[] = 'ocean_tons';
$unit[] = 'gal_us';
$unit[] = 'gal_uk';

$result = array_map(function ($item) {
  $item = preg_replace_callback('~_\K[a-z]{2}\z~', function ($m) {
    return '(' . strtoupper($m[0]) . ')';
  }, $item);
  return strtr($item, array('3' => '³', '_' => ' '));
}, $unit);

print_r($result);

pattern details:

~          # pattern delimiter
_          # literal _
\K         # keep out all on the left (the underscore here)
[a-z]{2}   # two lowercase letters
\z         # anchor for the end of the string
~          # delimiter

With the \K feature, only the two letters at the end will be replaced. The replacement of the underscore is the job of strtr() (a character translation function and more)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Compare one array with another and replace missing values php

분류에서Dev

Replace array values inside a document

분류에서Dev

replace key with new array values

분류에서Dev

PHP multidimensional array value replace with another value

분류에서Dev

Php merge duplicate array values in a multidimensional array php

분류에서Dev

How do I replace certain PHP values with my own text?

분류에서Dev

Accessing a PHP array object(stdClass) values

분류에서Dev

Push values to the same index of an array in PHP

분류에서Dev

PHP How to sum values of the array of the same key

분류에서Dev

How to access a PHP array with keys of inner values

분류에서Dev

Compare array values with same key in PHP

분류에서Dev

PHP delete values from Session Array

분류에서Dev

PHP multidimensional array sort matching values

분류에서Dev

str_replace doesn't work with array arguments on PHP

분류에서Dev

Convert programmatically list of values to object

분류에서Dev

PHP foreach change two-dimensional original array values

분류에서Dev

JS - PHP - Build an Array of Data From the Values of a Specific HTML Tag

분류에서Dev

How to iterate through values in an array containing arrays in PHP

분류에서Dev

Changing array in another class programmatically

분류에서Dev

How can I set multiple default values in an HTML select control using the values in a PHP array?

분류에서Dev

PHP array_replace는 $ _POST에서 확인 된 요소 만

분류에서Dev

특수 조건의 PHP array_replace (고급 음역)

분류에서Dev

need to access 3 values in a php array that contains 20, is a 3d array and I need to access the same values 60 times

분류에서Dev

Php Summing Array 2 Values Based On Array 1 Value 중복

분류에서Dev

PHP str_replace "and"

분류에서Dev

PHP preg_replace

분류에서Dev

PHP str_replace "and"

분류에서Dev

PHP Regex replace tags

분류에서Dev

Replace word with link in php

Related 관련 기사

  1. 1

    Compare one array with another and replace missing values php

  2. 2

    Replace array values inside a document

  3. 3

    replace key with new array values

  4. 4

    PHP multidimensional array value replace with another value

  5. 5

    Php merge duplicate array values in a multidimensional array php

  6. 6

    How do I replace certain PHP values with my own text?

  7. 7

    Accessing a PHP array object(stdClass) values

  8. 8

    Push values to the same index of an array in PHP

  9. 9

    PHP How to sum values of the array of the same key

  10. 10

    How to access a PHP array with keys of inner values

  11. 11

    Compare array values with same key in PHP

  12. 12

    PHP delete values from Session Array

  13. 13

    PHP multidimensional array sort matching values

  14. 14

    str_replace doesn't work with array arguments on PHP

  15. 15

    Convert programmatically list of values to object

  16. 16

    PHP foreach change two-dimensional original array values

  17. 17

    JS - PHP - Build an Array of Data From the Values of a Specific HTML Tag

  18. 18

    How to iterate through values in an array containing arrays in PHP

  19. 19

    Changing array in another class programmatically

  20. 20

    How can I set multiple default values in an HTML select control using the values in a PHP array?

  21. 21

    PHP array_replace는 $ _POST에서 확인 된 요소 만

  22. 22

    특수 조건의 PHP array_replace (고급 음역)

  23. 23

    need to access 3 values in a php array that contains 20, is a 3d array and I need to access the same values 60 times

  24. 24

    Php Summing Array 2 Values Based On Array 1 Value 중복

  25. 25

    PHP str_replace "and"

  26. 26

    PHP preg_replace

  27. 27

    PHP str_replace "and"

  28. 28

    PHP Regex replace tags

  29. 29

    Replace word with link in php

뜨겁다태그

보관