I am developing a website in php. I want to get a sub string in between two strings My main string is "ABCDEF ABCDEFGH ABCDEFGHIJ ABCDEFGHIJKLM ". I need to get the sub string in between last 2 s.(ABCDEFGHIJKLM)... Can you please help me for this issue.
Thanks Sateesh
$words = 'ABCDEF
ABCDEFGH
ABCDEFGHIJ
ABCDEFGHIJKLM
';
$words = explode('
', $words);
$word = $words[count($words) - 2];
You can use the Split()
function to tokenise the string. You can then locate the relevant part of the string....
http://php.net/manual/en/function.split.php
NOTE As advised below the use of this function is no longer recomended as it has been Deprecated from the PHP spec from 5.3 onwards. The correct answer would be to use the Explode()
function - as shown in the other answers above.
If the number of is unknown, you can use:
$string = "ABCDEF
ABCDEFGH
ABCDEFGHIJ
ABCDEFGHIJKLM
";
$words = explode("
", trim($string));
$last = array_pop($words);
echo $last;
Question: Do you want a newline (" ") or literally ' ' (backslash n)?
$lines = explode("
", trim($string));
$lastLine = $lines[count($lines) - 1];
or just (because when we trim the whitespaces away, we are looking for the last element)
$lastLine = end($lines);