Jump to content

String manipulation


alvin567

Recommended Posts

1) I don't see how that link provided any more details that what you already posted here...

2) Does the code I posted not do what you asked?

 

Here is a breakdown of what it does:

 

$string = "1811-011";
$string = preg_replace_callback( // function to match and replace a value with a LPI prefix and possible 2k if it is a 2k date
  '~^[0-9]{2}~',                 // match the first 2 digits in the string (example 19 in 1969)
  create_function(               // create anonymous function to handle the matched value
    '$m',                        // passing the matched value to the anonymous function
    'return                      // return the following:
      "LPI ".                    // the LPI prefix you want to add
      (($m[0]==20)?              // is the matched 2 digits 20?
        "2K":                    // if yes, add the 2K prefix 
        "");'                    // if no, nothing more to add
  ),                             // end of create_function call
  $string                        // the original string value to perform the regex on
);                               // end of preg_replace_callback call

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.