extracting specific information from a string in PHP

by: WanderingTechy Thursday, January 29th, 2009

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.

Related Posts

  • No Related Post

« | Home | »

Related Posts

Related Posts

 

Leave a Comment

Spam Protection by WP-SpamFree