There are two methods to extract information from a string. The first is using a token method. This will take a specified string and find all the information from the start of the string until a specified character is reached.
The strtok searched from the start of $bigstring until it found the first specified character. In this case, a space character. That value now belongs to the string called $firstpart. The search character could be a comma, letter, number, whatever. One more example for this command :
The next way to search a string is to use a substring method. This will take an index position of a string and take the specified number of characters after that position.
Wait, what? Index position?? Index position is a number. It specifies which character to start looking at in the string. The first character, second, third, and so on... One thing to remember though, PHP coding starts counting from zero, not one.
The index position 2 in the $bigstring value is g. B is at zero, i is at one, g is at two.
Now that you can specify a starting point, the next step is to specify the number of characters to view (including and) after that point. This example will show substr in action starting at position 7 and taking 4 characters. If it works correctly, the example string will hold a value of Wolf.
One more command to learn for now is strlen which stands for string length. There will be times when you don't know how long a string may be considering it often comes from a different source such as a database of form input. This command will help you determin how long a string is.
The example shows that $bigstring contains 11 characters. If you want to use this information in a substr command, remember to subtract one from the value. Remember, the substr starts counting from zero. The strlen command starts counting from one.
Yes, that is all a bit tricky, but once you get the hang of it, it may become rather useful.
<?php
$bigstring = "BigBad Wolf";
$firstpart = strtok($bigstring," ");
echo "$firstpart";
?>
$bigstring = "BigBad Wolf";
$firstpart = strtok($bigstring," ");
echo "$firstpart";
?>
BigBad
The strtok searched from the start of $bigstring until it found the first specified character. In this case, a space character. That value now belongs to the string called $firstpart. The search character could be a comma, letter, number, whatever. One more example for this command :
<?php
$bigstring = "Wolf,BigBad";
$firstpart = strtok($bigstring,",");
echo "$firstpart";
?>
$bigstring = "Wolf,BigBad";
$firstpart = strtok($bigstring,",");
echo "$firstpart";
?>
Wolf
The next way to search a string is to use a substring method. This will take an index position of a string and take the specified number of characters after that position.
Wait, what? Index position?? Index position is a number. It specifies which character to start looking at in the string. The first character, second, third, and so on... One thing to remember though, PHP coding starts counting from zero, not one.
$bigstring = "BigBad Wolf";
The index position 2 in the $bigstring value is g. B is at zero, i is at one, g is at two.
Now that you can specify a starting point, the next step is to specify the number of characters to view (including and) after that point. This example will show substr in action starting at position 7 and taking 4 characters. If it works correctly, the example string will hold a value of Wolf.
<?php
$bigstring = "BigBad Wolf";
$example = substr($bigstring,7,4);
echo "$example";
?>
$bigstring = "BigBad Wolf";
$example = substr($bigstring,7,4);
echo "$example";
?>
Wolf
One more command to learn for now is strlen which stands for string length. There will be times when you don't know how long a string may be considering it often comes from a different source such as a database of form input. This command will help you determin how long a string is.
<?php
$bigstring = "BigBad Wolf";
$stringlength = strlen($bigstring);
echo "The length of the string is $stringlength.";
?>
$bigstring = "BigBad Wolf";
$stringlength = strlen($bigstring);
echo "The length of the string is $stringlength.";
?>
The length of the string is 11.
The example shows that $bigstring contains 11 characters. If you want to use this information in a substr command, remember to subtract one from the value. Remember, the substr starts counting from zero. The strlen command starts counting from one.
Yes, that is all a bit tricky, but once you get the hang of it, it may become rather useful.

