I've been working on a simple JavaScript function that dynamically puts together a preview URL.
The only problem is if you try to make a title such as 'Example - Test' it ends up becoming 'example___test' where as I want to replace two or more instances of dashes with a single dash.
function cms_url(){ var file0 = document.getElementById('meta_title').value.toLowerCase(); var file1 = file0.replace(/ /g, '-'); var file2 = file1.replace(/_/g, '-'); var file3 = file2.replace(/--/g, '-'); var file4 = file3.replace(/--/g, '-'); alert(file4); document.getElementById('cms_url').firstChild.nodeValue=document.getElementById('meta_section').options[document.getElementById('meta_section').selectedIndex].getAttribute('title')+'/'+file2;}
<?php
if (!empty($_POST['regex_line'])) print_r(@$_POST['regex_line']); else $_POST['regex_line']='__XXX______XX___Y_X_Z__123';
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="regex_line" id="regex_line" type="text" value="<?php echo @$_POST['regex_line']; ?>" />
<input type="submit" />
</form>
<script type="text/javascript">
function remove_double__(str)
{
var regex_str = "(__)+";
var re = new RegExp(regex_str,'gm');
c=0;
if (str.match(re))
{
while (str.match(re))
{
c++; if (c>1000) break;
str = str.replace(RegExp.lastParen,'_');
}
return (str);
}
return (str);
}
element = document.getElementById('regex_line');
text = element.value;
element.value = remove_double__(text);
</script>
<?php
what you will need is probably just the javascript part.
Note : the c counter is safeyt net, breaking out if more than 1000 loops occur. you can remove it if not needed, no harm leaving it ( unless you are expecting a thousand __) .