Forum Moderators: coopster
I was wondering how you would stip unwanted characters from a string.
For example if someone entered rma5110 and i wanted to strip out all text and just leave the numbers every time how would i do that.
When i post the data to the database i want to add back in the RMA so in the database it would look like RMA5110. I have the following code which changes lower case to uppercase and if no rma was enter it adds it, but if someone enters rms instead of rma i get this posted to the database RMArms5110.
------------------------------------------------------------
// Look to see if RMA/rma was entered then replace it with RMA
if (strncasecmp($RMA, 'RMA', 3) == 0)
{
echo "RMA ENTERED AT FRONT";
$RMA = strtoupper($RMA);
}
// Look to see if RMA/rma was entered if RMA not presant then add RMA to $RMA.
if (strncasecmp($RMA, 'RMA', 3)!= 0)
{
echo "NO RMA ENTERED AT FRONT";
$a = "RMA";
$RMA = $a . $RMA; // now $RMA contains "RMA"
}
substr [uk.php.net] should do the trick:
$string = 'rma5110';
echo substr($string,3);
This of course assumes 3 characters before the numbers start.
dc
but if someone enters rms instead of rma i get this posted to the database RMArms5110
As for this, if you have the same length values being put in you can check the length of the string with strlen() [us3.php.net] in order to see if there are letters before the number. If this isn't the case then you can use other various string functions or use Regular Expressions [etext.lib.virginia.edu] which I'm not all that familiar with.
I found this one helped alot
This stips all alphanumeric values out leaving you with just the numbers.
function clean_string($string)
{
return ereg_replace("[^[:digit:]]", "", $string);
}
Then i do this which adds RMA back in.
$RMA = clean_string ($RMA); // calls the above function
$a = "RMA";
$RMA = $a . $RMA; // now $RMA contains "RMA"