0

I have a String with some specific abbreviations. The String can contain one, more or no abbreviation. Then I have an Array with the abbreviation as elements.

I'm searching for a solution to give the matched abbreviation in a new array back.

For example:

$abbreviation = array('HSGE', 'DFVC');
$string = 'This is a abbreviation HSGE and here is the other DFVC";

The output should be:

$output = ([0] => HSGE [1] => DFVC);

Maybe it's easy but I am new with this.

2
  • What about the output in this string 'This is a abbreviation HSGE and here is the other DFVC, another DFVC one and one more HSGE' ? Commented Jan 12, 2018 at 15:52
  • 1
    How is this related to mysql? Commented Jan 12, 2018 at 15:56

3 Answers 3

4

A simple solution using array_intersect:

array_intersect(explode(' ',$string), $abbreviation)
Sign up to request clarification or add additional context in comments.

Comments

0

You can create a loop foreach element of your abbreviations array and search for the position of every element in the string with strpos, then if you find it, you can add the abbreviation to the resulting array with array_push.

You can find what you need here:

http://php.net/manual/en/function.strpos.php

http://php.net/manual/en/control-structures.foreach.php

http://php.net/manual/en/function.array-push.php

Comments

0

You should use strpos to retrieve the position of the abreviation in the string. http://php.net/manual/en/function.strrpos.php

If the abreviation is not in the string then strpos will return FALSE

So a solution is to loop through your abbreviation array and check if this abreviation is used in the string with strpos.

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.