3

How to remove special characters?

foreach ($login_and_logout_list['rval'] as $k => $v) {
  $login_time[] = $v['login_time'];
  $logout_time[] = $v['logout_time'];
  $create_time[] = $v['create_time'];
  $i++;
}
var_dump($login_time);

What i am getting in var_dump

array (size=3)
  0 => string '{09:46},{09:48}'
  1 => string '{05:04},{07:04},{08:40},{08:54}'
  2 => string '{05:20}'

What i want in var_dump is

array (size=3)
  0 => string '09:46 | 09:48'
  1 => string '05:04 | 07:04 | 08:40 | 08:54'
  2 => string '05:20'
  • 2
    Possible duplicate of [Remove all special characters except space from a string using JavaScript](https://stackoverflow.com/questions/6555182/remove-all-special-characters-except-space-from-a-string-using-javascript) – alseether Nov 17 '17 at 07:08
  • 1
    You can str_replace on php.. Since your code is on php and not javascript. – Eddie Nov 17 '17 at 07:09

1 Answers1

2

I assume you want to port your code from PHP to JavaScript.

var a = ['{09:46},{09:48}', '{05:04},{07:04},{08:40},{08:54}', '{05:20}']

a = a.map(o => {
   return o.replace(/(}\,{)/mg, ' | ').replace(/(}|{)/mg, '');
})


console.log(a);

Here's the PHP solution:

<?php 

$a = ['{09:46},{09:48}', '{05:04},{07:04},{08:40},{08:54}', '{05:20}'];

$a = array_map( function($o){

    $o = preg_replace('/(}\,{)/m', ' | ', $o);

    $o = preg_replace('/(}|{)/m', '', $o);

    return $o;

}, $a);

var_dump($a); 

// output
// array(3) { 
//      [0]=> string(13) "09:46 | 09:48" 
//      [1]=> string(29) "05:04 | 07:04 | 08:40 | 08:54" 
//      [2]=> string(5) "05:20" 
// }
Adam Azad
  • 11,171
  • 5
  • 29
  • 70