1

i have sample data like this :

enter image description here

i want to get integer in the red box. i now i can using explode(). but I am confused to apply in my problem. how can i apply explode to my problem?

this my code :

$ikan=$_POST['ikan']; //example : Katombo,(30 basket)
$kata=explode("(", $ikan);
2
  • do a echo explode(" ",$kata[1]); Commented Nov 20, 2017 at 4:28
  • 1
    A regex would probably be better. You just want an integer, or an integer in ()s, or an integer preceding basket? \d is an integer. regex101.com/r/1yW54y/1 and see preg_match documentation. Commented Nov 20, 2017 at 4:28

4 Answers 4

1

We use explode two times after we explode the first array.

CODE

$ikan= 'Katombo,(30 basket)';
$kata= explode(" ", explode("(", $ikan)[1])[0];
echo $kata;

OUTPUT

30

UPDATED ANSWER If you want to loop it and use regex. you can use the ff:

$ikan= array('Katombo,(30 basket)', 'Layang,(0 basket)', 'Loka-loka, (0 basket)', 'Tongkol, (0 basket)');
$kata = array();
foreach($ikan as $value){
     $kata[] = explode(" ", explode("(", $value)[1])[0];
}

Using preg_match_all and regex

$ikan= array('Katombo,(30 basket)', 'Layang,(0 basket)', 'Loka-loka, (0 basket)', 'Tongkol, (0 basket)');
$kata = array();
foreach($ikan as $value){
    preg_match_all("/(\d+)/", $value,$num);
    $kata[] = $num[0][0];
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try the following code:

<?php 
$ikan='Katombo,(30 basket)'; //example : Katombo,(30 basket)
$kata=explode("(", $ikan);
$baskests = explode(" ", array_pop($kata));
echo $baskests[0];

Comments

0

If you want all integer values in one array... i have added some more in @bluetree answers Try this:-

$array = array('Katombo,(30 basket)','Layang,(0 basket)');
foreach ($array as $key => $value) {
        $kataArray[]= explode(" ", explode("(", $value)[1])[0];
}
echo "<pre>"; print_r($kataArray); die;

3 Comments

i am not get what you are trying to say @chris85
see the question of iqbal he clearly mentions he wants integer values @chris85
Yes, I see, this answer doesn't have anything to do with integers though. See 3v4l.org/a1O4J
0

You can also do this

<?php
$ikan = str_split('Katombo,(30 basket)');
print_r($ikan);
echo $ikan['9'].$ikan['10']

//and the seconde method.

  foreach ($ikan as $value){
            if (is_numeric($value)){
                  echo $value;
            }
        }
?>

2 Comments

Why make an array of characters? If the user already knows what position the numbers they can just use them as is. 3v4l.org/tqXGd
Easily distinguishable position the numbers.For me.And use it in the second method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.