0

I'm working with some strings that appear in the following formats:

 $string = 'Acme Building Company - BZ-INTERNAL 1';
 $string = 'Acme Building Company - TRP-SHOP-1';
 $string = 'Acme Building Company - ZJG-INTERNAL 2';

I now need to get to the characters in the middle of these strings and parse them out, so in the above examples I would end up with the following:

  BZ
  TRP
  ZJG

I'm looking for the most dynamic approach so I don't have to hardcode any substitution strings but haven't been able to come up with anything so far.

2
  • Will the start of the string always be the same length ? In this case Acme Building Company - Commented Apr 12, 2017 at 5:31
  • preg_match('~-(.*?)-~', $string, $output);echo $output[1]; Commented Apr 12, 2017 at 5:36

6 Answers 6

2

Use explode(); PHP function like so:

<?php
$string = 'Acme Building Company - BZ-INTERNAL 1';
$tmp = explode('-', $string);
$middle = trim($tmp[1]);
echo $middle;

The output of the above code segment will be: BZ

The syntax goes explode(DELIMITER, THE_STRING); Which returns an array.

So when we explode the given string BZ cones in the array index 1. You can use the same approach for all your strings, maybe in a loop.

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

Comments

0

Im keen to wait and see how other people would approach this question. But if you can guarantee the string will always be formatted the way you have shown I would personally try:

$exploded = explode('-', $string);
$intials = trim($exploded[1]);

Comments

0

Using a regular expression will do this quite nicely.

$match = null;
if (preg_match('/- (.*?)-/', $string, $matches)) {
    $match = $matches[1];
}

Comments

0
<?php preg_match("/(.*)-(.*)-(.*)/", $input_line, $output_array); echo trim($output_array[2]);?>

or

<?php preg_match("/(.*) - (.*)-(.*)/", $input_line, $output_array); echo $output_array[2];?>

Comments

0

The second call to strtok() can isolate the desired substring.

Notice that the character mask on the second call includes a space so that the following value is effectively left trimmed.

Code: (Demo)

$string = 'Acme Building Company - BZ-INTERNAL 1';

strtok($string, '-');
var_export(strtok('- '));
// 'BZ'

Comments

-1

@user982124 if your string format will be the same so yes you can explode() it and at the 1th index you will get your characters like:

<?php
 $string1 = 'Acme Building Company - BZ-INTERNAL 1';
 $string2 = 'Acme Building Company - TRP-SHOP-1';
 $string3 = 'Acme Building Company - ZJG-INTERNAL 2';

 $strArr1 = explode("-", $string1);
 echo trim($strArr1[1])."<br>";

 $strArr2 = explode("-", $string2);
 echo trim($strArr2[1])."<br>";

 $strArr3 = explode("-", $string3);
 echo trim($strArr3[1])."<br>";

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.