One could use this regular expression (regex):
/#(\w+)/
along with preg_match_all() which performs a global search for data matching a regex, and stores what it finds in an array, as follows:
<?php
$str = "Two 9-dram clear styrene tubes with snap-on caps, 1 x 2-3/4″ I.D. (25.2 x 68mm), are supplied as containers. Additional or replacement tubes #8909 are available.
As a safety precaution, BioQuip recommends using the #1135Y HEPA filter with this aspirator.
Also see #1135X aspirator syringe bulb. It allows users to aspirate specimens without inhaling particulate matter.";
$pat = "/#(\w+)/";
if ( preg_match_all( $pat, $str, $matches )) {
$popped = array_pop( $matches );
list( $alpha, $beta, $gamma ) = $popped;
echo "The IDs are as follows:
$alpha
$beta
$gamma
";
}
See demo
This regex specifies a hashtag followed by parentheses to "remember" one or more word characters, i.e. numeric or alphabetical data. By enclosing this expression with parentheses, the hashtag will be excluded and the remaining data stored in $matches[1]
. Once $matches contains data, you are done unless you wish to utilize $matches[1]
to create three unique variables and optionally display their values.
You could dispense with using a regex and use the following code with the above-indicated value of $str
:
<?php
$ids=[];
$a = explode("#",$str);
array_shift( $a );
$ids = array_map( function( $e ) {
return explode(" ",$e)[0];
},$a);
print_r($ids);
See demo
By avoiding preg_match_all(), the second solution may offer the advantage of better performance. Alternately, you can split the array returned by explode() into a list that only includes relevant information, and then create an array of the list variables for array_map(), as follows:
<?php
$ids=[];
list(,$a,$b,$c) = explode( "#",$str );
$ids = array_map(function( $e ) {
return explode(" ",$e )[0];
},[ $a,$b,$c ]);
print_r( $ids );
See demo