Forum Moderators: coopster

Message Too Old, No Replies

Striping unwanted characters from a string

         

bodycount

11:47 am on May 16, 2006 (gmt 0)

10+ Year Member



Hi again

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"
}

dreamcatcher

1:46 pm on May 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi bodycount,

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

eelixduppy

7:29 pm on May 16, 2006 (gmt 0)




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.

bodycount

8:07 am on May 17, 2006 (gmt 0)

10+ Year Member



Thanks for your help.

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"