split string on multiple char like '+' '-' '*' '/'

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

rads
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Sep 10, 2012 8:07 am

split string on multiple char like '+' '-' '*' '/'

Postby rads » Mon Sep 10, 2012 8:11 am

Hi All,
I want to split a string like "67* 90+ 86 -23" into tokens as '67' '*' '90' '+' '86' '-' '23'
Plss help me do it...

us2rn4m2
New php-forum User
New php-forum User
Posts: 17
Joined: Thu Aug 09, 2012 1:28 pm

Re: split string on multiple char like '+' '-' '*' '/'

Postby us2rn4m2 » Fri Sep 14, 2012 1:52 pm

Hi,

Something like this ?

Code: Select all

<?php

$a = "67* 90+ 86 -23";
$b = str_split($a, 2);

echo '<pre>';
print_r($a);
echo '</pre>';

/* Result:   
   Array
   (
      [0] => 67
      [1] => *
      [2] => 90
      [3] => +
      [4] => 86
      [5] =>  -
      [6] => 23
   )   
 */

$c = '';

foreach($b as $key => $value) {
   $v  = trim($value);
   $c .= " '$v' ";
}

echo $c;
//   Result: '67' '*' '90' '+' '86' '-' '23'


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 2 guests