Results 1 to 4 of 4

Thread: simple regex question

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default simple regex question

    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.
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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()?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    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.
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •