Substring

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)

Get String Before & After Character/String If Present

	$mystring = "url/index.php"
	$parts = explode("/",$mystring);
	$mystring1 = $parts['0'];      //String before
	$mystring2 = $parts['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)
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published.