Jump to content

substr function


Bendoon

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/285621-substr-function/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/285621-substr-function/#findComment-1466321
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/285621-substr-function/#findComment-1466329
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.