<?php
$myString=substr([COLOR="Blue"]my[/COLOR]Stri[COLOR="Red"]ng[/COLOR],0,2);
echo $myString;
?>
The code above produces “my” which is 2 letters from the left.
I like to produce “ng” which is 2 letters from the right.
<?php
$myString=substr([COLOR="Blue"]my[/COLOR]Stri[COLOR="Red"]ng[/COLOR],0,2);
echo $myString;
?>
The code above produces “my” which is 2 letters from the left.
I like to produce “ng” which is 2 letters from the right.
instead of 0, use srtlen() minus however many characters from the end you need as your starting point in substr()
The code below is one of trials for it, but it causes an error.
<?php
$myString="myString";
$myString=substr(myString,[COLOR="Red"]srtlen($myString)-2[/COLOR],2);
echo $myString;
?>
How can I fix the code above?
Try this:
$myString=“myString”;
$myString=substr($myString, -2);
rtfm (read the fine manual)
or another way
$myString = 'mystring';
echo substr($myString,strlen($myString)-2);
but lampcms’s way is shorter