OsakaWebbie
Programmer
Is there any function (used to massage the output in a SELECT) that works like PHP's ereg_replace, i.e. finds all occurrences of a "regular expression" match and replaces it with a specified string?
Here's the background. I have database data (song lyrics and other accompanying info) that I output in various formats, using specifications that are themselves stored in the database - that way I have the maximum flexibility to customize formats without having to change the actual code. I am now adding the capability of including guitar chords with the lyrics - how it is stored in the DB is like: "Happy [G]Birthday to [D7]you..." (sorry for the silly example, but it's a song everybody knows!). When I just display it on the screen for viewing song information, I use PHP, so it's no problem. If the user wants to see the chords, I replace "[" and "]" with appropriate HTML & CSS to display them correctly (the chords go above the letter they precede - CSS is great for this!). If they want only lyrics, I replace "[whatever]" with nothing, which requires regexp rather than a simple string replace. That's in PHP, so no problem. But when I make a song set designed for printing, I do everything in SQL because of all those flexible formats that I don't want to hardcode. The replace() function in MySQL is used very heavily in these outputs to turn plain text into nice HTML with CSS classes, but I can't seem to find a way in MySQL to combine the flexibility of REGEX with the functionality of replace(). Can someone help? The server is currently using MySQL 3.23 (a bit old, I know, but it's not my machine, so I can't upgrade it).
For those of you who like to see the nitty-gritty code, here is what I use now (no consideration for chords):
It's hard to read, but it works. (The triple replace for various end of line characters is to cover all combinations for different operating systems - if I could do regexp replace, that could also be simplified.) For the case where the user does want the chords, I can do it by adding two more replace() calls - "[" to "<span class=chord>" and "]" to "</span>". But what about when the user does not want the chords? I need to remove the opening and closing bracket and the unknown content between them - the regular expression would be "\[[^\[]*\]", but if I can't use regexp, I'm stumped.
Here's the background. I have database data (song lyrics and other accompanying info) that I output in various formats, using specifications that are themselves stored in the database - that way I have the maximum flexibility to customize formats without having to change the actual code. I am now adding the capability of including guitar chords with the lyrics - how it is stored in the DB is like: "Happy [G]Birthday to [D7]you..." (sorry for the silly example, but it's a song everybody knows!). When I just display it on the screen for viewing song information, I use PHP, so it's no problem. If the user wants to see the chords, I replace "[" and "]" with appropriate HTML & CSS to display them correctly (the chords go above the letter they precede - CSS is great for this!). If they want only lyrics, I replace "[whatever]" with nothing, which requires regexp rather than a simple string replace. That's in PHP, so no problem. But when I make a song set designed for printing, I do everything in SQL because of all those flexible formats that I don't want to hardcode. The replace() function in MySQL is used very heavily in these outputs to turn plain text into nice HTML with CSS classes, but I can't seem to find a way in MySQL to combine the flexibility of REGEX with the functionality of replace(). Can someone help? The server is currently using MySQL 3.23 (a bit old, I know, but it's not my machine, so I can't upgrade it).
For those of you who like to see the nitty-gritty code, here is what I use now (no consideration for chords):
Code:
SELECT concat('<p class=lyrics>',replace(replace(replace(replace(replace(Lyrics,' ',' '),'\r\n','\n'),'\r','\n'),'\n','</p>\n<p class=lyrics>'),'<p class=lyrics></p>','<p class=smallspace> </p>'),'</p>\n')