开发者

PHP string - Using short codes to add to system but how to do it?

开发者 https://www.devze.com 2023-03-12 19:02 出处:网络
I want to take a short string, and manipulate it in PHP so various parts of it can be inserted into various parts of the database.

I want to take a short string, and manipulate it in PHP so various parts of it can be inserted into various parts of the database.

It's a way I want to add contact information into my system.

I use a series of short codes to denote what I want the string to do. So:

  • NC = New contact
  • DESC = A description of the contact
  • MB = A cell phone number
  • HM = A home phone number
  • BS = A business number
  • EM = An email address

So for example:

$string = "NC John Doe DESC President USA MB 555123123 HM 12341234 EM john@example.net"

And so on. I can't see why there needs to be a limit on the length.

What would you do in PHP to make this easy? I want to开发者_高级运维 add phone numbers to the phone table, emails to the email table and contacts to the contact table.

I also have short codes for updates (to add more numbers to existing contacts)

  • UPD - Update contact X
  • AMB - Add cell
  • (Axx - "A" is add)
  • I won't bother with delete/change

$string = "UPD John Doe AMB 55512341233 AEM john2@example.net"

I know it's a bit of an odd way to get things into a system, but it's quick to type when I'm on the run.


I Agree with Michael J.V. about using an array. It would be easy to add/change actions

Example:

function getAction($action) {
    $actions = array(
        'NC'    => 'New contact',
        'DESC'  => 'A description of the contact',
        'MB'    => 'A cell phone number',
        'HM'    => 'A home phone number',
        'BS'    => 'A business number',
        'EM'    => 'An email address',
    );
    return $actions[$action];
}

$selected = 'DESC';
echo 'Action Selected: '.getAction($selected)."\n";


You'll probably want to look at regular expression pattern matching, but you're also going to need to have pretty much bulletproof security for anything interacting with the database once you've extracted your commands.

So if inserting/updating/deleting a phone number make sure the "body" of the command is numbers only (or only numbers and spaces and + symbols if you're accepting them) and so on.


Following the OP's suggested methodology I would do this with an array (as pointed in another answer) but serializing it before going into the database and unserializing when coming out.

Another option is creating a custom class that handles the same as the array but can also validate and sanitize whatever goes in and out of the DB.

Having said that. Is there a particular reason why you don't wont to use DB columns to store this data and use a related status table for your custom codes? It will make your software much easier to upgrade and maintain (not to mention debug!).

0

精彩评论

暂无评论...
验证码 换一张
取 消