Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

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

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

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...
rads
New php-forum User
New php-forum User
 
Posts: 1
Joined: Mon Sep 10, 2012 8:07 am

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'
us2rn4m2
New php-forum User
New php-forum User
 
Posts: 13
Joined: Thu Aug 09, 2012 1:28 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.