Return to Snippet

Revision: 60454
at November 9, 2012 18:08 by jprochazka


Updated Code
// String to extract string from.
$string = 'This is a string to extract <tag>contents between tags</tag> from.';

// Call the function.
echo extractString($string, '<tag>', '</tag>');

// Function that returns the string between two strings.
function extractString($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);
}

// Value Echoed: contents between tags

Revision: 60453
at November 9, 2012 18:07 by jprochazka


Updated Code
// String to extract string from.
$string = 'this is a string to extract <tag>contents between tags</tag> from.';

// Call the function.
echo extractString($string, '<tag>', '</tag>');

// Function that returns the string between two strings.
function extractString($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);
}

// Value Echoed: contents between tags

Revision: 60452
at November 9, 2012 18:05 by jprochazka


Initial Code
// Function that returns the string between two strings.
function extractString($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);
}

Initial URL


Initial Description
Returns the contents of a string between two unique strings. Useful for returning the portion of a string found between two tags such as <tag> and </tag>

Initial Title
Return a string between two strings or tags.

Initial Tags


Initial Language
PHP