RyanJ Posted November 29, 2005 Posted November 29, 2005 Hi everyone! I'm making a chemistry website and to save databse space (A lot when you have lots of things too add) I decided to make a pattern match replacer for some of the information, specifically the electron configurations. If you don't know what the electron configuration format is then here it is. nTe Where n = any 1-2 digit number, T is either s,p,d or f. If T = s then e can be any positive number number less than 2. If T = p then e can be any positive number number less than 6. If T = d then e can be any positive number number less than 10. If T = f then e can be any positive number number less than 14 I have eneted the information in to the databse in that format, my problem comes here: I need the e part to be in superscript so I need to do a pattern match to find and replace e with <sup>e</sup>. My best efforts failed and now I turn too you for help, has anyone got (Or can make) a piece of code that will do that? Values for the format can be like this: [Rn]5f147s27p1 should be made too: [Rn]5f147s27p1 1s22s2 should be replaced by 1s22s2 Cheers, Ryan Jones
RyanJ Posted November 29, 2005 Author Posted November 29, 2005 Why not just use LaTeX or MiMeTeX? Its not my server and because of the space they require to install etc. the host will not install them unless they get enough demand. Pitty because this was my first thought too. Cheers, Ryan Jones
Cap'n Refsmmat Posted November 29, 2005 Posted November 29, 2005 MiMeTeX would work perfectly. It doesn't require extra software, it's just a CGI that you drop in your cgi-bin folder.
RyanJ Posted November 29, 2005 Author Posted November 29, 2005 MiMeTeX would work perfectly. It doesn't require extra software, it's just a CGI that you drop in your cgi-bin folder. It requires you too run on the root admin server account, because I'm not it does not work! Pitty because I have also tried this too! Cheers, Ryan Jones
Klaynos Posted November 29, 2005 Posted November 29, 2005 If each nTe secontion is in the db individualy (and not a full elements list) then: May I suggest a series of regular expressions, replacing s,p,d,f with s<sup>,p<sup>,d<sup>,f<sup>. and then adding </sup> to the end of each string If not the easiest way with the least thought is one regular expression for each number and letter...
RyanJ Posted November 29, 2005 Author Posted November 29, 2005 If each nTe secontion is in the db individualy (and not a full elements list) then: May I suggest a series of regular expressions' date=' replacing s,p,d,f with s<sup>,p<sup>,d<sup>,f<sup>. and then adding </sup> to the end of each string If not the easiest way with the least thought is one regular expression for each number and letter...[/quote'] Good suggestion but I worked it out Here is the code incase anyone else will be able to make use if it in the future: function FixElectronConfig($ConfigString){ $String = $ConfigString; $String = preg_replace('/((\d)+)s(([1-2])?)(\s)?/', '\\1s<sup>\\3</sup>', $String); $String = preg_replace('/((\d)+)p(([0-6])?)(\s)?/', '\\1p<sup>\\3</sup>', $String); $String = preg_replace('/((\d)+)d((\d){1,2})(\s)?/', '\\1d<sup>\\3</sup>', $String); $String = preg_replace('/((\d)+)f((\d){1,2})(\s)?/', '\\1f<sup>\\3</sup>', $String); return $String; } It accepts strings in the format: [Rn]1s2 2s2 2p6 3d10 4f14 The element name in brackets is optional, it can work with out it. It kills the spaces and it makes the "e" part superscript, it works with all strings I have tested. If you make a better one or find a problem with this one please post here Cheers, Ryan Jones
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now