Find jobs - Adult ADHD - Indian television shows news - Myspace Layouts - Debt Consolidation

PDA

View Full Version : Need help on replacement of line


Domen Lombergar
Mar 17th 2008, 3:24 pm
hey everyone.

I have a bit of a problem - i'm using javascript to automatically generate web-friendly urls from the titles, but i've run into some issues with special characters

This is the code:

function setAlias(objvalue){
document.newpage.newpagealias.value = makesafe(objvalue);
}

function makesafe(text){
text = text.replace(" / ","_");
text = text.replace("'","");
text = text.replace("\"","");
text = text.replace("/","_");
text = text.replace("'","_");
text = text.replace(" - ","_");
text = text.replace(" ","_");
text = text.replace("è","c");
text = text.replace("š","s");
text = text.replace("ž","z");
text = text.replace("È","C");
text = text.replace("Š","S");
text = text.replace("Ž","Z");
text = text.replace( "&", "and");
text = text.replace( "%", "Percent");
text = alphanumeric(text);
text = text.replace("____","_");
text = text.replace("___","_");
text = text.replace("__","_");
text = text.toLowerCase();
return text;
}

function alphanumeric(userval)
{

for(var j=0; j<userval.length; j++)
{
var eachChar = userval.charAt(j);
var hh = eachChar.charCodeAt(0);
if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || (eachChar =="_") || (eachChar =="-"))
{
//
}
else
{
userval = userval.replace(eachChar,"_");
}
}
return userval;
}


The two input fields that are connected here are the following two:

<input type="text" class="text" name="newpagetitle" value="" size="56" maxlength="255" onchange="setAlias(this.value)" />
and
<input type="text" class="text" name="newpagealias" value="" size="16" maxlength="50" />

basically it should convert all čšžČŠŽ into cszcsz but it does this only for the first of them, then turns the rest into _

So my question is basically...

Does anyone know what would need changing here so that this would work in converting all special characters into regular letters

and ... does anyone know if it's also possible to remove the ending underscore (example from "word_" to "word")?

Thanks so much!

vpguy
Mar 17th 2008, 9:41 pm
Keep in mind that the javascript replace() function uses regular expressions, it is not a simple "replace(this, withThat)" type of function like you would find in VB or C#.

I also see a lot of messy and redundant things in your code. For example, trying to replace four underscores, then three, then two with a single underscore. A loop would be able to strip out multiple underscores more effectively.

And instead of checking for uppercase and lowercase special characters, then converting the string to lowercase as the last step, it is better to convert the string to lowercase first and then check for lowercase special characters, because the toLowerCase() function will convert Č to č, Š to š, and Ž to ž for you.

Here is how I think it should probably be implemented, although others might recommend some improvements. Get rid of the alphanumeric() function since its purpose is handled in the revised function below:


function makesafe(text)
{
text = text.toLowerCase();
text = text.replace(/č/g, "c");
text = text.replace(/š/g, "s");
text = text.replace(/ž/g, "z");
text = text.replace(/&/g, " and ");
text = text.replace(/%/g, " percent ");
text = text.replace(/\W/g, "_");

while (text.indexOf("__") >= 0)
{
text = text.replace(/__/g, "_");
}

if ((text.length > 0) && (text.lastIndexOf("_") == (text.length - 1)))
{
text = text.substr(0, text.length - 1);
}

return text;
}


To test it thoroughly, use the following input string:

čšžČŠŽ & 25% with 'quotes', "spaces" and _____ underscores ___

The above test string will be converted to:

cszcsz_and_25_percent_with_quotes_spaces_and_underscores

Domen Lombergar
Mar 17th 2008, 11:37 pm
Wow. Thanks. Um. Wow.

What's your paypal? :)