Log in

View Full Version : simple regex question



james438
02-27-2012, 11:04 AM
At one point I had this idea that it is better to limit the frequency that my website has to execute a PCRE command. To this end I created a script that would format my articles before submitting them to the database. In my case I was replacing hyphens with actual dashes before being stored in the database. In my particular case I am pretty sure dash is a non alphanumeric character.

Would it be better in this case to use PCRE to format the text after it is retrieved from the database despite the increased PCRE usage?

I am curious which is the better coding practice.

djr33
02-27-2012, 11:09 AM
Whatever way is most efficient. If you ever need the raw code again, it's best to store it raw (if it's something you could edit, for example). But if it's a final version that you won't need to have in the raw format again, then go ahead and convert it when you save to the database. There's no harm in that.

The only thing I'm not sure about is why you're using regex to change a character-- why not str_replace()?

james438
02-27-2012, 11:41 AM
Thanks for the answer.

I try to avoid regex whenever possible so as to use it only when I need it. I actually like regex to some degree. When writing articles I usually have some form of css, javascript, or html stored with it to format the text. str_replace would replace the hyphens in that code as well as the article, which I do not want to do.

djr33
02-27-2012, 02:49 PM
Makes sense. Maybe there's a way around it, but by the time you used a few operations to isolate the text you want to modify, it probably wouldn't save much time.

Either way, I think pre-formatting for use in the DB is your best option.