Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting Multiple Image Numbers from a String

Status
Not open for further replies.

kennygadams

Programmer
Jan 2, 2005
94
US
Hello PHP-ers,

I'm trying to get all the Image Numbers from a variable called $string, with all the other characters removed.

The image numbers are always numeric and formatted like 0000-0000-0000-0000.

Here is the code I'm working with...
Code:
$string = '[COLOR=red]0001-0611-2520-3615[/color] \t \r123 [COLOR=red]0001-0603-1919-3126[/color] abc >[COLOR=red]0001-0611-2223-1711[/color]</\n';
preg_match_all('/([^\d\d\d\d\-\d\d\d\d\-\d\d\d\d\-\d\d\d\d]+)/i', $string, $matches);
print_r($matches);
Any help is greatly appreciated.

Thanks.

Kenny Adams
 
>I'm trying to get all the Image Numbers from a variable called $string, with all the other characters removed.
But the script seems doing the contrary? or I miss something. Try this?
[tt]
preg_match_all('/(?<!=\B)\d{4}-\d{4}-\d{4}-\d{4}(?!\B)/', $string, $matches);
[/tt]
I've made a provision of negative lookahead and lookbehind of word boundary so as to prepare you for further refinement to, for instance, only excluding digit or letter or something else according to the need.
 
just playing around i came up with
Code:
<?
$string = '0001-0611-2520-3615 \t \r123 0001-0603-1919-3126 abc >0001-0611-2223-1711</\n';

preg_match_all('/([\d]{4}-?){4}/', $string, $matches);
print_r($matches);
?>
 
Thanks tsuji and jpadie. Your codes work exactly the same.

Here is the output from both codes...

Code:
Array ( [0] => 0001-0611-2520-3615 [1] => [2] => [3] => [4] => [5] => [6] => 123 [7] => 0001-0603-1919-3126 [8] => [9] => [10] => [11] => [12] => [13] => 0001-0611-2223-1711 [14] => [15] => [16] => [17] => )

Can the code below be tweaked a little more so that it only allows correctly formatted Image Numbers, (0000-0000-0000-0000), into the array?

Code:
$string = '0001-0611-2520-3615 \t \r123 0001-0603-1919-3126 abc >0001-0611-2223-1711</\n';

preg_match_all('/([\d]{4}-?){4}/', $string, $matches);
print_r($matches);

Thanks.


Kenny Adams
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top