Hi u all, I set up a code that trims a variable, but now it trims to much if the trim value is found twice in the variable. Don't know what or where the hickup in the code is.... Here is the code: $volgnr = ($row -> volgnr); $_GET[knsa_nr] = rtrim($_GET[knsa_volgnr_sch], "$volgnr"); PHP: e.g. if $volgnr = 8 and $_GET[knsa_volgnr_sch] = 1364988 the rtrim() returns $_GET[knsa_nr] as 13649 where it should return 136498. What did i do wrong in my code?? Thanxs lampie1978
I'm not sure rtrim is the right function to be using. http://uk.php.net/rtrim maybe http://www.php.net/strpos would be better? What are you actually wanting to do?
The problem is rtrim() is greedy and it will remove all characters which match the trim list until it finds one which does not match. A less greedy alternative is preg_replace. Try this: $volgnr = ($row -> volgnr); $_GET[knsa_nr] = preg_replace( "/$volgnr$/", '', $_GET[knsa_volgnr_sch], 1); PHP: I added the count parameter for good measure. If your version of PHP is too old, remove it. It works fine both ways in my tests.
Ah right, that's something I didn't know - You learn something new every day Bit strange if it allows you to do all those though. Lee.
Although those unquoted strings work, it's not a good idea to do that. It only works if you don't have a constant already with that name and because the parser is continually checking the constant list for each unquoted string, there is a performance hit, too...
Clancey, Thanxs the preg_replace() did the trick. Didn't know if it could do the trick when $volgnr has double figures, but it works fine. The rest also thanxs for your input.. Lampie1978
I am glad I could help. It will work fine with multiple numbers. If $volgnr is 456 and the other number is 99456, it will trim off the 456, leaving 99.