alvin567 Posted June 5, 2012 Share Posted June 5, 2012 How do I change 2011-011 to LPK-2k11-011??? Quote Link to comment https://forums.phpfreaks.com/topic/263683-string-manipulation/ Share on other sites More sharing options...
requinix Posted June 5, 2012 Share Posted June 5, 2012 1. Start with the string "LPK-2k". 2. Grab the first four digits. 3. Subtract 2000. 4. Append to the string. 5. Append a "-". 6. Grab the last three digits. 7. Append to the string. Quote Link to comment https://forums.phpfreaks.com/topic/263683-string-manipulation/#findComment-1351307 Share on other sites More sharing options...
tarunpahuja Posted June 5, 2012 Share Posted June 5, 2012 How do I change 2011-011 to LPK-2k11-011 $count = 1; str_ireplace('20','LPK-2k','2011-011',$count) LPK-2k11-011 Quote Link to comment https://forums.phpfreaks.com/topic/263683-string-manipulation/#findComment-1351326 Share on other sites More sharing options...
alvin567 Posted June 5, 2012 Author Share Posted June 5, 2012 Ok,there is a format issues, for example 1994 I can to change to if i have LPI 94-038 2012-011 I want to change LPI 2K11-011 lets make it more versatile! Quote Link to comment https://forums.phpfreaks.com/topic/263683-string-manipulation/#findComment-1351327 Share on other sites More sharing options...
alvin567 Posted June 5, 2012 Author Share Posted June 5, 2012 How any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/263683-string-manipulation/#findComment-1351440 Share on other sites More sharing options...
tarunpahuja Posted June 5, 2012 Share Posted June 5, 2012 Would you please like to tell what are you trying to do and make your scenario more understandable? Quote Link to comment https://forums.phpfreaks.com/topic/263683-string-manipulation/#findComment-1351455 Share on other sites More sharing options...
.josh Posted June 5, 2012 Share Posted June 5, 2012 $string = "1969-011"; $string = preg_replace_callback('~^[0-9]{2}~',create_function('$m','return "LPI ".(($m[0]==20)?"2K":"");'),$string); Quote Link to comment https://forums.phpfreaks.com/topic/263683-string-manipulation/#findComment-1351465 Share on other sites More sharing options...
alvin567 Posted June 6, 2012 Author Share Posted June 6, 2012 Can you make it more readable? Quote Link to comment https://forums.phpfreaks.com/topic/263683-string-manipulation/#findComment-1351536 Share on other sites More sharing options...
alvin567 Posted June 6, 2012 Author Share Posted June 6, 2012 To give more details: http://answers.yahoo.com/question/index?qid=20120604183959AAo9Qhk Quote Link to comment https://forums.phpfreaks.com/topic/263683-string-manipulation/#findComment-1351537 Share on other sites More sharing options...
.josh Posted June 6, 2012 Share Posted June 6, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/263683-string-manipulation/#findComment-1351542 Share on other sites More sharing options...
alvin567 Posted June 6, 2012 Author Share Posted June 6, 2012 thats great!! Quote Link to comment https://forums.phpfreaks.com/topic/263683-string-manipulation/#findComment-1351543 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.