I want to remove the first character of a string like so,
$a1 = "A10";
$a2 = substr($a1,1,strlen($startsem1)-1);
but I want to retrieve that removed "A" later on. Is there a way I can "cut" or "copy" that removed "A" to some variable?
Did you mean:
$a1 = "A10";
$a2 = substr($a1,1,strlen($a1)-1);
echo $a1[0]; // output "A" eg old value for you
echo $a2; // output "10"
PHP supports array like syntax for strings too so $a1[0]
(zero based index as in array) extracts first letter from string.
Use substr
again.
$a3 = substr($a1, 0, 1);
or as others commented, just split the string on whatever delimiter you're interested in using.
You can save it in its own variable (here I use string indexing to get the first character, but you may just as well use substr
):
$saved_a = $a1[0];
Also note that you may skip the third parameter to substr if you want to get the rest of the string:
$a2 = substr($a1, 1);
For fun, you could do this:
$a1 = "A10";
$split = str_split( $a1); // Split the string into an array
$first_char = array_shift( $split);
$everything_else = implode( '', $split);
You can do directly this way:-
<?php
$result1 = "A10";
$result2 = $result1{0};
echo "$result1{0} : " . $result1{0} . "
";
echo "$result2 : " . $result2 . "
";
?>
A10{0} : A
A : A
Refer LIVE DEMO