1

I have a string like this:

$string = "1,4|2,64|3,0|4,18|";

Which is the easiest way to access a number after a comma?

For example, if I have:

$whichOne = 2;

If whichOne is equal to 2, then I want to put 64 in a string, and add a number to it, and then put it back again where it belongs (next to 2,)

Hope you understand!

5 Answers 5

1

genesis'es answer with modification

$search_for = 2;
$pairs = explode("|", $string);
foreach ($pairs as $index=>$pair)
{
    $numbers = explode(',',$pair);
    if ($numbers[0] == $search_for){
        //do whatever you want here
        //for example:
        $numbers[1] += 100; // 100 + 64 = 164
        $pairs[index] = implode(',',$numbers); //push them back
        break;
    }
}
$new_string = implode('|',$pairs);
Sign up to request clarification or add additional context in comments.

Comments

1
$numbers = explode("|", $string);
foreach ($numbers as $number)
{
    $int[] = intval($number);
}

print_r($int);

2 Comments

Do I have to loop through the entire array if I just want to edit one number? I just gave an example, my originally string is much much longer. Is there no way to search for example "2," and then take the following numbers before the "|"?
I meant 2 with a comma after "2,"
1
$string = "1,4|2,64|3,0|4,18|";
$coordinates = explode('|', $string);
foreach ($coordinates as $v) {
    if ($v) {
        $ex = explode(',', $v);
        $values[$ex[0]] = $ex[1];
    }
}

To find the value of say, 2, you can use $whichOne = $values[2];, which is 64

1 Comment

I seemed to have missed the second part of your question; Vitaliy's answer should suit your needs.
1

I think it is much better to use the foreach like everyone else has suggested, but you could do it like the below:

$string = "1,4|2,64|3,0|4,18|";
$whichOne = "2";

echo "Starting String: $string <br>";

$pos = strpos($string, $whichOne);

//Accomodates for the number 2 and the comma
$valuepos = substr($string, $pos + 2);

$tempstring = explode("|", $valuepos);
$value = $tempstring[0];  //This will ow be 64

$newValue = $value + 18;

//Ensures you only replace the index of 2, not any other values of 64
$replaceValue = "|".$whichOne.",".$value;
$newValue = "|".$whichOne.",".$newValue;

$string = str_replace($replaceValue, $newValue, $string);

echo "Ending String: $string";

This results in:

Starting String: 1,4|2,64|3,0|4,18|
Ending String: 1,4|2,82|3,0|4,18|

You could run into issues if there is more than one index of 2... this will only work with the first instance of 2.

Hope this helps!

2 Comments

Thanks! What does the + 2 mean in substr?
Hey Daniel, the script is finding the first position of 2. Which will return the starting point of 2. So adding 2 means I will move the index past the number 2 and then pas the comma so your pos index position is now starting at the 6 of 64. So it essentially eliminates everything before the 64 so when you run your explode, it will be the first index of that array, so we can access it using $array[0]. Hope that made sense...cause I'm not sure I understand what I just wrote... ;)
1

I know this question is already answered, but I did one-line solution (and maybe it's faster, too):

$string = "1,4|2,64|3,0|4,18|";

$whichOne = 2;
$increment = 100;

echo preg_replace("/({$whichOne},)(\d+)/e", "'\\1'.(\\2+$increment)", $string);

Example run in a console:

noice-macbook:~/temp% php 6642400.php
1,4|2,164|3,0|4,18|

See http://us.php.net/manual/en/function.preg-replace.php

Comments

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.