0

I have got a string in PHP.

(check_nt!USEDDISKSPACE!-l g -w 90 -c 95)

I want to split the string at the first ! (between check_nt and USEDDISKSPACE) into two arrays.

I thought to use strpos to get the position of the first sign. I don´t know how to split the string at the numeric position I get from strpos as return value.

3
  • 8
    What language are you using? Commented Jul 29, 2014 at 9:18
  • Have you considered using a regular expression for this? Commented Jul 29, 2014 at 9:20
  • possible duplicate of Split string to specific array position c# Commented Jul 29, 2014 at 9:50

3 Answers 3

1

I assume that you use PHP, so consider substr in addition to strpos:

$str = "(check_nt!USEDDISKSPACE!-l g -w 90 -c 95)";

$pos = strpos($str, "!");

$a = substr($str, 0, $pos);    // $a = "(check_nt"
$b = substr($str, $pos + 1);   // $b = "USEDDISKSPACE!-l g -w 90 -c 95)"

Other approach using explode:

$str = "(check_nt!USEDDISKSPACE!-l g -w 90 -c 95)";

$arr = explode("!", $str, 2);  // $arr[0] = "(check_nt"
                               // $arr[1] = "USEDDISKSPACE!-l g -w 90 -c 95)"

Note that both approaches remove the ! to split at.

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

1 Comment

Your solution is good. Thanks!!
1

Use the following regular expression:

^(\([^!]+)([ -~]+)$

This will give you:

Grp 1.      `(check_nt`
Grp 2.      `!USEDDISKSPACE!-l g -w 90 -c 95)`

Comments

0

Use regular expressions:

/^.*!/

will match to the first part of the string, while

/!.*$/

to the second part. But the implementation depends on the language you use.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.