HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

how to update pre tag to code tag in DB

jackmaessenjackmaessen ✭✭✭
edited December 2015 in General Banter

In vanilla 2.1 i did use geshi for syntaxhighlighting. In 2.2 i want to use creativesyntaxhighlighter for it.
In geshi, all the code is between pre tags like below:

<pre lang="html" line="1">code goes here</pre>
same for php, js and css

In creativesyntax i use the code tag to highlight:

<code lang="html"> code goes here</code>
same for php, js and css

How can i update all the <pre lang="html" line="1"></pre> to <code lang="html></code>

i already tried to run this but without success:
UPDATE GDN_Comment SET Body = '<code lang="php">' WHERE Body = '<pre lang="php" line="1">';

Tagged:

Comments

  • hgtonighthgtonight ∞ · New Moderator

    I would dump the db to an sql file, do my replacements there, then import the SQL file.

    It doesn't look like MySQL provides a regex function, but I am no MySQL expert.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • UPDATE GDN_Comment SET Body = '<code lang="php">' WHERE Body = '<pre lang="php" line="1">';

    This is wrong becuase you are saying the entire body is is <pre lang="php" line="1">, and the entire body is to be replaced

    You want

    UPDATE GDN_Comment SET Body = REPLACE(Body, '<pre lang="php" line="1">', '<code lang="php">') WHERE Body LIKE '%<pre lang="php" line="1">%';

    The problem is with this is you don't get the full power regular expression based replace in MySQL. You can me more specific using a replace on a dump.

    You are only replacing the first part, which means you will have buck of stray </pre> which you have to replace. <pre> is a legit tag to use. So you could end up with a problem of unmatched closing tags.

    There is a good arguments which say using <code> like this incorrect if you want a code block it should be defined

    <pre><code>[code goes here]</code></pre> and inline code just uses <code>.

    grep is your friend.

  • I would dump the db to an sql file, do my replacements there, then import the SQL file.

    Brilliant idea @hgtonight !
    I dumped to DB to localhost.sql, modified all the tags with this tool: https://findandreplace.codeplex.com/
    Import the sql file again and in a couple of minutes the job was done!
    Thank God there is https://findandreplace.codeplex.com/ <3

  • hgtonighthgtonight ∞ · New Moderator

    Glad you got it working @jackmaessen :D

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

Sign In or Register to comment.