From start of string
echo substr($MyVariable, 3, 1); //Returns 1 character starting from the 4th character (0 is first character)

echo substr($MyVariable, 3); //Returns all characters starting from the 4th character (0 is first character)
From end of string
echo substr($SoftwareVersion, -2); //Returns the last 2 charaters of the string (negative values start from string end)

Get String Before & After Character/String If Present

	$MyString= "url/index.php";
	$StringParts= explode("/", $MyString);
	$StringBefore= $StringParts['0'];      //String before
	$StringAfter = $StringParts['1'];      //String after

Remove First Word In A String

echo substr(strstr("Abc Def Ghi"," "), 1);

Remove Last Word In A String

$last_space_position = strrpos($text, ' ');
$text = substr($text, 0, $last_space_position);

Get String Before First Occurrence Of A String

	$my_output_string = strstr($my_string,"[TAGENDMARKER]", TRUE);	//Returns string up to start of first match, excluding the string being matched

Get String After First Occurrence Of A String

  $my_output_string = strstr($my_string,"[TAGSTARTMARKER]", FALSE);	//Returns string after start of first match, including the string being matched

Neat way of getting just the string after the search string:

  $InputString = "something/members/hello/";
  $OutputString = explode('/members/', $InputString);
  $OutputString = $OutputString[1];       //$OutputString now = "hello/"

Getting String Between Strings

function get_string_between_strings ($string, $start, $end)
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0)
			return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);

$fullstring = 'hello blah blah [tag]dog[/tag]';
$parsed = get_string_between_strings($fullstring, '[tag]', '[/tag]');

echo $parsed; // (returns: dog)

Convert version number to Major.Minor

    $SoftwareVersion = $Result['SoftwareVersion'];
    $SoftwareVersionMinor = substr($SoftwareVersion, -2);
    $SoftwareVersionMajor = ($SoftwareVersion - $SoftwareVersionMinor) / 100;
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.


Your email address will not be published.