2

Using PHP to call a Java app, the Java app returns a string encapsulated by "[....]", I need to remove the outside brackets and maintain what's inside -a mix of integers, strings, and other special chars like (, etc.. I'm using sscanf successfully later in the process, so I'm a little lost as to why this particular step is so difficult.

Input:

 $str = [(123)[334.5 : 765.1] string]

Non-functional code:

$format = '[%s]';
sscanf($str,$format,$output);

Later in the process I'm successfully using sscanf to parse the inside string using:

$format = '(%d) [%f : %f] %s';

I'm missing something big here...

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
jbrain
  • 561
  • 3
  • 7
  • 18

3 Answers3

4

Assuming that the string is always wrapped in [...] and you don't need to check for incorrectly formatted strings, you can simply remove the first and last characters:

$str = substr($str, 1, -1);

Edit: This is more reliable than using trim() and friends because ltrim/rtrim will remove all leading/trailing square brackets, not just the first and last one. This could be a problem if your string ever contains something like "[[value]..]" - you'd be left with "value]..".

Another edit: If you do need to check if the string is formatted correctly, it's easy to verify that the first and last characters are square brackets:

if ((substr($str, 0, 1) == '[') && (substr($str, -1) == ']')) {
    $str = substr($str, 1, -1);
} else {
    //Something went wrong - the string is not wrapped in [brackets].
}
Janis Elsts
  • 754
  • 3
  • 11
  • +1 and OP is failing probably because there are multiple [ ] in the input or possibly REGEX character – Waygood Aug 17 '12 at 13:30
  • 1
    +1 My method *will* remove double square brackets, but at the same time, this method will remove the first char if the bracket *isn't* there. – Fluffeh Aug 17 '12 at 13:31
  • Nice, thank you. I tried both methods, each worked well but I'm going with this one for 'clarity'. – jbrain Aug 17 '12 at 13:37
  • Fluffeh: Good point. Luckily, it's trivial to check for that. Edited. – Janis Elsts Aug 17 '12 at 13:39
1

You could use the ltrim() and rtrim() functions to get rid of the external brackets:

$str = '[(123)[334.5 : 765.1] string]';
$str = ltrim($str, "[");
$str = rtrim($str, "]");
echo $str;
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
-1

Source: https://www.php.net/manual/en/function.sscanf.php#:~:text=s%20stops%20reading%20at%20any%20whitespace%20character.

format
The interpreted format for string, which is described in the documentation for sprintf() with following differences:

[...]

  • s stops reading at any whitespace character.

Your format string is failing because there are whitespace characters between the braces.

Assuming your string substring at the end of your input string contains no closing braces, you can instead use a negated character class containing a closing brace.

Code: (Demo)

$str = '[(123)[334.5 : 765.1] string]';

var_export(
    sscanf($str, '[(%d)[%f : %f] %[^]]]')
);

Output:

array (
  0 => 123,
  1 => 334.5,
  2 => 765.1,
  3 => 'string',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136