Jump to content

search and replace with regular expressions


marm

Recommended Posts

How could I replace all the commas with "commas followed by a space" only within the keywords meta tag ? (using dreamweaver's regular expressions)

 

Turn this:

 

<meta name="Keywords" content="word1,word2,word3,word4,word5,word6" />

 

into this:

 

<meta name="Keywords" content="word1, word2, word3, word4, word5, word6" />

Here's PHP, but I realized you want to do this within Dreamweaver, which I've no idea of (and doubt that it can). Let me know if I need to move this to another forum. Sorry about that.

 

<pre>
<?php

$html = <<<HTML
<meta name="Keywords" content="word1,word2,word3,word4,word5,word6" />
<meta name="NotKeywords" content="word1,word2,word3,word4,word5,word6" />
HTML;

function commafy_keywords ($match) {
	if ($match[0] = preg_split('/(content=")([^"]+)(")/', $match[0], -1, PREG_SPLIT_DELIM_CAPTURE)) {
		$match[0][2] = preg_replace('/(?<=,)(?! )/', ' ', $match[0][2]);
	}
	return implode('', $match[0]);
}

echo htmlspecialchars(preg_replace_callback('/<meta[^>]+name="Keywords"[^>]*>/', 'commafy_keywords', $html));

?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.