4

JavaScript has two handy substring functions: substring(from,to) and substr(start,length).

This means I can decide "when I get my substring starting from position X, do I want to specify what string position to end on or how many characters long the substring is?"

(One nice usage of the from, to version is to use search() to determine both positions.)

PHP's substr() lets you specify the length of the substring. Is there a built-in string function to let you specify the character position to end on?

2
  • I think you mean that PHP's substr() does the latter... it takes the length as a parameter. Commented Aug 26, 2010 at 21:07
  • @Felix Kling - yes, that's what I meant. Thanks. Commented Aug 26, 2010 at 21:13

4 Answers 4

12

I think this question is more about logic. You can accomplish both with the substr() function.

For instance. for the First substring(from, to):

$string = 'Foo Bar!';
$from   = 2;
$to     = 5;
$final_string = substr($string, $from, $to - $from);

And the second substr(start,length):

$string = 'Foo Bar!';
$start  = 2;
$length = 5;
$final_string = substr($string, $start, $length);
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, ok, that makes sense. I just wondered if I was missing something in the jungle of PHP string functions. :)
4

As an addition to Nick's answer, you can always write the substring function yourself:

function substring($string, $from, $to){
    return substr($string, $from, $to - $from);
}

I also have a PHP file with some JavaScript like function as the strtruncate() function and include them to the PHP script I need them for.

Comments

2

Just in case you need a PHP port of JavaScript's "substring" method here's a reliable port I had to write time ago:

/**
 * PHP port of JavaScript's "substring" function
 * Author: Tubal Martin http://blog.margenn.com
 * Tests: http://margenn.com/tubal/substring/
 *
 * @param string $str
 * @param int    $from index
 * @param int    $to index (optional)
 */
private function substring($str, $from = 0, $to = FALSE)
{
    if ($to !== FALSE) {
        if ($from == $to || ($from <= 0 && $to < 0)) {
            return '';
        }

        if ($from > $to) {
            $from_copy = $from;
            $from = $to;
            $to = $from_copy;
        }
    }

    if ($from < 0) {
        $from = 0;
    }

    $substring = $to === FALSE ? substr($str, $from) : substr($str, $from, $to - $from);
    return ($substring === FALSE) ? '' : $substring;
}

Comments

0

substr(string, start, length) is a standard usage of the function link

1 Comment

Sorry, my "former" and "latter" stuff was confusing. I was asking about how to specify the end position.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.