extracting specific information from a string in PHP

I have a bunch of entries in a database where one field (varchar) is of this format

First name Last Name (AccountId-ItemId)

For example

“John Smith (1342-314)”

I needed to extract the Account and Item id’s from this.

if (preg_match('/\\([0-9]{1,4}-[0-9]{1,4}\\)/', $source_data, $regs)) {
        $match = $regs[0];
        }

Which gives you the 1324-314, Then I simply

$bang=explode ("-",$match);
$itemid=$bang[1];
$accountid=$bang[0];

I highly recommend RegExpBuddy if you want to create custom regular expressions. It’s a very handy tool.

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.