I’m trying to replace underscores with dashes within an href attribute that is in a large amount of text coming from a database:
Existing Text:
Hello, my name is <a href="http://example.com/joe_smith">joe smith</a> and I
eat pizza with my friend <a href="http://example.com/john_doe">john doe</a>.
Output:
Hello, my name is <a href="http://example.com/joe-smith">joe smith</a> and I
eat pizza with my friend <a href="http://example.com/john-doe">john doe</a>.
Since it’s currently in a mysql database, I assume it would be faster if I could perform the action using an sql statement, but if that’s not possible I would like to do it using a php regex.
I do NOT want to replace underscores that are in the regular text for one reason or another. Only the ones that are within the href.
MySQL’s regexes are for searching only. They do not support replacement at all. You can use them to find the records that need fixing, but then you’re limited to basic string operations within mysql only for actually changing the records.
You’d be better off pulling the matched records into PHP and doing the changes there. Which of course then brings up the use of regexes on html… don’t do it. Use PHP’s DOM instead for the actual manipulations.