Shalini
Shalini

Reputation: 27

Splitting a string variable into parts

I have a variable which contains values like

1000*2/750

I want it to break into parts like

1000
2
750

So it can be stored in db in different columns.

Upvotes: 1

Views: 67

Answers (5)

Dhara Parmar
Dhara Parmar

Reputation: 8101

If you're looking to split the string with multiple delimiters, perhaps preg_split would be appropriate.

Try:

$input= "1000*2/750";
$output = preg_split("/(\*|\/)/", $input);

Using explode function:

function explodeX( $delimiters, $string )
{
    return explode( chr( 1 ), str_replace( $delimiters, chr( 1 ), $string ) );
}

$list =  "1000*2/750";

$exploded = explodeX( array('*', '/'), $list );

print_r($exploded);

Upvotes: 0

phreakv6
phreakv6

Reputation: 2165

$input= "1000*2/750";
$v = explode('-',str_replace(array('*','/'),'-',$input));
print_r($v);

Upvotes: 0

aarju mishra
aarju mishra

Reputation: 710

You can use this coding.It definitely gonna works:

<?php

print_r(split("[/.*]", "1000*2/750"));

?>

Upvotes: 0

splash58
splash58

Reputation: 26153

If you want find groups of digit, split string by non-digit sequence

$input= "1000*2/750";
$output = preg_split("/[\D+]/", $input);
print_r($output);

Upvotes: 1

Gautam Jha
Gautam Jha

Reputation: 1473

    $q="1000*2/750";
    $str=str_replace('/',' ',str_replace('*',' ',$q));
    $exp=explode(" ",$str);
    echo $exp[0];
    echo "<br>";
    echo $exp[1];
    echo "<br>";
    echo $exp[2];

this code will help you to do this..(str_replace)

this result is also achieved by preg_replace.

Upvotes: 0

Related Questions