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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.