18

Say I have the following: 44-xkIolspO

I want to return 2 variables:

$one = "44";
$two = "xkIolspO";

What would be the best way to do this?

2
  • Remember, the manual lists every function you could ever want: php.net/ref.strings Commented Jan 19, 2011 at 3:17
  • Well, unless you know how many variables you must get, list() may render quite useless. Manual assignment is better. In such cases, use a foreach() to create an array of values. Commented Apr 11, 2019 at 5:56

3 Answers 3

44

Try this:

list($one, $two) = split("-", "44-xkIolspO", 2);

list($one, $two) = explode("-", "44-xkIolspO", 2);
Sign up to request clarification or add additional context in comments.

5 Comments

Awesome, except that split is deprecated. A more future-proof solution would probably be explode.
use preg_split("/-/", "44-xkIolspO");
Updated the post to use explode. Hope its not deprecated yet.
According to micro-optimization.com/explode-vs-preg_split using preg_split is 69% faster.
@MikeKormendy when you read the article it says "f2 is slower than f1 by 68.73% " where f2 is the preg_split and f1 is the explode. So, unless I'm missing something obvious, the article is saying that explode is 69% fater than preg_split
11

PHP has a function called preg_split() splits a string using a regular expression. This should do what you want.

Or explode() might be easier.

    $str = "44-xkIolspO";
    $parts = explode("-", $str);
    $one = $parts[0];
    $two = $parts[1];

1 Comment

I don't think preg_split is anywhere near necessary in this case. Regular expression parsing is much less efficient than a delimiter string.
1

There are a number of native, single-function solutions to split on your delimiting character. Demo

  1. sscanf() offers two styles of implementation -- reference variables as trailing parameters or if there are no references it will return the captures as an array. The power is in the placeholders -- %d will return the leading number from the sample input as an integer-type value. Recommended if your string is predictably formatted.

    sscanf($text, '%d-%s', $one, $two);
    

    or destructure the return:

    [$one, $two] = sscanf($text, '%d-%s');
    
  2. explode() is the most commonly used tool for this task and certainly reliable for thus task. Its return value will need to be destructured as individual variables.

    [$one, $two] = explode('-', $text);
    
  3. str_getcsv() can be modified to split on hyphens instead of commas. While this approach works on the sample string, this function provides some magic behaviors regarding escaping which may not be desirable for general-use. Not recommended.

    [$one, $two] = str_getcsv($text, '-');
    
  4. preg_split() will work just like explode() if fed the literal hyphen in a regular expression, but less efficiently than all other mentioned techniques. It has helpful behavior modifying flags but none which are helpful for this task. Not recommended.

    [$one, $two] = preg_split('/-/', $text);
    

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.