Bendoon Posted January 23, 2014 Share Posted January 23, 2014 Hi im trying to use the substr function in php. I have a string called $finalString and im trying to grab the last character of that string the line of code im using is : $startIndex = substr($finalString, -1); it wont work giving the error : Warning: substr() expects parameter 2 to be long, string given in /users/2017/bfm3/public_html/cs1109/lab10/markovnew.php on line 34 Quote Link to comment https://forums.phpfreaks.com/topic/285621-substr-function/ Share on other sites More sharing options...
davidannis Posted January 23, 2014 Share Posted January 23, 2014 I cut and pasted your code and surrounded it with my own. <?php $finalString="abcd"; $startIndex = substr($finalString, -1); echo $startIndex; ?> and it works (output is d). Could you have another line with a substr() in it? Is this really line 34? Did you cut and paste or retype the line when posting? Quote Link to comment https://forums.phpfreaks.com/topic/285621-substr-function/#findComment-1466321 Share on other sites More sharing options...
Bendoon Posted January 23, 2014 Author Share Posted January 23, 2014 ah ok yes there is another line i use the substr but that wasnt giving trouble until i put in the new substr. $sub = substr($textarea, $startIndex, $ordernumber ); is line 34 why is that giving the error then Quote Link to comment https://forums.phpfreaks.com/topic/285621-substr-function/#findComment-1466323 Share on other sites More sharing options...
Bendoon Posted January 23, 2014 Author Share Posted January 23, 2014 the php manual states i can have 3 parameters Quote Link to comment https://forums.phpfreaks.com/topic/285621-substr-function/#findComment-1466324 Share on other sites More sharing options...
Bendoon Posted January 23, 2014 Author Share Posted January 23, 2014 So ive got a loop and basically the loop adds a character to the end of $finalString after each loop, i want to then grab the last character of that $finalString and thats what im attempting to do Quote Link to comment https://forums.phpfreaks.com/topic/285621-substr-function/#findComment-1466325 Share on other sites More sharing options...
Bendoon Posted January 23, 2014 Author Share Posted January 23, 2014 OK i think its because where i have $startIndex on this line: $sub = substr($textarea, $startIndex, $ordernumber ); $startIndex contains an actual character and not a number. Is there a way to instead of using substr to extract the character in that space extract the number of how many characters it is in from the start? So if "hello" was the string and "e" was selected it would contain the number "2" ? Quote Link to comment https://forums.phpfreaks.com/topic/285621-substr-function/#findComment-1466329 Share on other sites More sharing options...
Ch0cu3r Posted January 23, 2014 Share Posted January 23, 2014 (edited) Use strpos to find the position of a character Edited January 23, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285621-substr-function/#findComment-1466332 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.