2
$string = '763:74
74:274
177:474';

$number = array('763', '74', '177');

I need to get the corresponding values of $number from $string. Example: if number = 763 I need string value 74. If number = 74 I need string value 274 and so on.

What right now what I'm doing is exploding $string at : character, looping and matching.

Looking for some better solution.

7
  • Right now what i am doing is exploding $string at :, looping and matching. Show the code. I bet you're really close to the solution! Commented Nov 21, 2015 at 8:05
  • @Rizier123 Solution I have achieved but I want to avoid loop. It slows down the whole process if $string data increases. Commented Nov 21, 2015 at 8:07
  • You can create an array, where the number before the colon is the key and the number after the colon the value. So then you can just do: echo $lookupArray[$yourNumber]; Commented Nov 21, 2015 at 8:09
  • $string data is coming from another source. Commented Nov 21, 2015 at 8:13
  • 1
    3v4l.org/F5t2P As an example Commented Nov 21, 2015 at 8:16

3 Answers 3

2

You can simply convert your string into an array simply using preg_replace_callback like as

$string = '763:74
74:274
177:474';

$result = [];
preg_replace_callback('/(\d+):(\d+)/m',function($m)use(&$result){
    $result[$m[1]] = $m[2];
},$string);

print_r($result);

output:

Array
(
    [763] => 74
    [74] => 274
    [177] => 474
)

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

This should work for you:

First I split the string with preg_split() and use a new line as delimiter (\n), where I also consume all spaces (\s*) on the right and left side, so you don't have them in the key:

preg_split("/\s*\n\s*/", $string)

Then I go through each pair of numbers (x:y) with array_map() and explode() it by a colon. So you end up with an array like this:

Array
(
    [0] => Array
        (
            [0] => 763
            [1] => 74
        )

    [1] => Array
        (
            [0] => 74
            [1] => 274
        )

    [2] => Array
        (
            [0] => 177
            [1] => 474
        )

)

At the end I use array_column() to say, that you want to use the 0st column as key and the 1st column as value.

Code:

<?php

    $string = '763:74
    74:274
    177:474';

    $result = array_column(
        array_map(function($v){
            return explode(":", $v);
        }, preg_split("/\s*\n\s*/", $string)
    ), 1, 0);

    print_r($result);

?>

output:

Array
(
    [763] => 74
    [74] => 274
    [177] => 474
)

Comments

1

You can do it using preg_match - first you find the position of the key string, then you find the pair with that key to know the length of the pair.:

function find_in_string($n,$string){
    preg_match("/(^|\s+)($n)(?=:)/",$string,$m1,PREG_OFFSET_CAPTURE);
    preg_match("/(^|\s+)($n\:\d+)/",$string,$m2);
    return substr($string,$m1[2][1]+ strlen($n)+1,strlen($m2[2])-strlen($n));
}

Example:

$string = '763:74
74:274
177:474';
foreach(array('763', '74', '177') as $n){
    echo find_in_string($n,$string);
}

Output:

74
274
474

Demo:

http://sandbox.onlinephpfunctions.com/code/320e8ba1b47e62b75459aaf70f07994fe531b101

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.