patternphpMinor
Modifying stress markers in Cyrillic words
Viewed 0 times
markerswordscyrillicstressmodifying
Problem
This is used to change a Cyrillic word with a stress marker into the same Cyrillic word but with the stress marker separately represented as a number. As I want to return two values I pass two parameters by reference. I assume there is at most only one stress marker per word.
What could I improve here?
What could I improve here?
function seperateStress($word,&$cleanWord,&$stressLetter){
$stressLetter=-1;
$cleanWord="";
for($i=0;$i";
$stressLetter=$i;
$i=$i+5;
}else{
$cleanWord .=$word[$i];
//echo $word[$i].":".$i."";
}
}
//return array($cleansedWord,$stressedLetter);
}
//Sample Test run
$stressWord= "велосипе́ды";
$noStressWord="";
$stressLetter=-1;
seperateStress($stressWord,$noStressWord,$stressLetter);
echo $noStressWord."::".$stressLetter."";Solution
Normally I would look at the PHP manual and make sure you aren't recreating a function that already exists, but I have no experience with non ASCII characters so i don't feel comfortable with the terminology to look this up. I would suggest that you make sure of this though. This sounds like something that might already exist somewhere. As to your function, there are a few things that jumped out at me.
First are those references. They are a powerful tool, but I would be careful using them. They are relatively difficult to spot and not everyone knows what they do. Sometimes its better just to use plain return statements, but that is something you will have to decide based on the scope of this project. If its something that isn't likely to be used by others, then this is fine, otherwise you might think about changing it.
When incrementing a variable by more than one, a more elegant way of doing so is by using the following syntax. By the way, where did the five come from? Unless cyrillic always has three digit entities, then you run the risk of missing some of the escape sequence. For example
First are those references. They are a powerful tool, but I would be careful using them. They are relatively difficult to spot and not everyone knows what they do. Sometimes its better just to use plain return statements, but that is something you will have to decide based on the scope of this project. If its something that isn't likely to be used by others, then this is fine, otherwise you might think about changing it.
When incrementing a variable by more than one, a more elegant way of doing so is by using the following syntax. By the way, where did the five come from? Unless cyrillic always has three digit entities, then you run the risk of missing some of the escape sequence. For example
nbsp is a four digit entity. I would use strpos() or something similar to find the position of the semicolon instead.$i += 5;$stressLetter and $cleanWord definitions are redundant. You set them to the same values before the function was called. The only reason this would be necessary is if you revert back to using a return value instead of references, and then you should remove those parameters from the parameter list and from the procedural code before the function call.Context
StackExchange Code Review Q#15321, answer score: 2
Revisions (0)
No revisions yet.