This is a bit old - you may or may not notice the date on this post but it's over a year old. That doesn't mean it's not useful but we all know how fast things chance on the web so there's a chance that techniques and technologies described here could be a little dated.

ChatGPT might be a game changer for coding

So I’ve had on my to do list for a while a bit of coding to take an input text and add ‘rel=”nofollow”‘ to any links that don’t already have it set. In theory it’s a straightforward thing to do, either use a regex or something like DOMDocument to parse the text. But it’s a bit of a faff and I haven’t gotten round to it yet.

I have though having a play around with ChatGPT and chatting to a friend he said “have you used it for coding yet”. So I thought this one might be a good place to start checking it out and wow is it good.

ChatGPT coding

As you can see from the screenshot above in a matter of a minute or so I have my function. But looking at that I thought “I wonder if it’d be better with regex”. So why not ask?

It’s a simple as typing “rewrite the function using regex instead of DOMDocument” and here’s the new output.

function add_nofollow_to_links_regex($text) {

// Define the regex pattern to match tags and their attributes
$pattern = '/]*?\s+)?href=(["\'])(.*?)\1(?:[^>]*?\s+)?(.*?)?>/i';

// Replace all tags with modified versions that include rel="nofollow"
$modified_text = preg_replace_callback($pattern, function($match) {
$href = $match[2];
$attributes = $match[3];
$nofollow = 'rel="nofollow"';
// If the link already has a rel attribute, add nofollow to it
if (preg_match('/\brel=([\'"])/i', $attributes, $rel_match)) {
$rel_value = $rel_match[1] . $nofollow . ' ' . $rel_match[1];
$attributes = preg_replace('/\brel=([\'"]).*?\1/i', $rel_value, $attributes);
}
// If the link doesn't have a rel attribute, add it
else {
$attributes .= ' ' . $nofollow;
}
// Return the modified
tag
return '
';
}, $text);

return $modified_text;
}

// Example usage:
$input_text = "This is a
link to a website.";
$output_text = add_nofollow_to_links_regex($input_text);
echo $output_text;

I really think ChatGPT does have the power to be game changing here, there are a few caveats though, one being I think you need to have some idea of what you’re expecting back and I’d probably make sure to test what you’re getting quite thoroughly as well. But if you learn how to ask and refine your requests you can get some great stuff back.

There’s a great podcast from Drowned in Sound that whilst it approaches it from a musician perspective gives some great info about how to use these new AI tools

https://podcasts.apple.com/gb/podcast/dis005-how-music-can-embrace-the-power-of-ai/id1037405920?i=1000601175249

Check out David Boyle’s book prompt as well (he’s the one interviewed in the podcast)

https://prompt.mba/