Viewed   78 times

I need to delete the first 3 letters of a string and the last 3 letters of a string. I know I can use substr() to start at a certain character but if I need to strip both first and last characters i'm not sure if I can actually use this. Any suggestions?

 Answers

4

Pass a negative value as the length argument (the 3rd argument) to substr(), like:

$result = substr($string, 3, -3);

So this:

<?php
$string = "Sean Bright";
$string = substr($string, 3, -3);
echo $string;
?>

Outputs:

n Bri
Sunday, August 7, 2022
4

I ran a test and seems like the first solution is faster. Here is the code for testing it:

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

function solution1($text)
{
    for($i = 0; $i < 10000; $i++)
        list($module) = explode('::',$text);
}

function solution2($text)
{
    for($i = 0; $i < 10000; $i++)
        $module = substr($text, 0, strpos($text, '::'));
}

$text = 'AdministrationControllerUserController::Save';

$time_start = microtime_float();

solution1($text);

$time_end = microtime_float();
$time = $time_end - $time_start;

echo "Did solution1 in $time seconds.n";

$time_start = microtime_float();

solution2($text);

$time_end = microtime_float();
$time = $time_end - $time_start;

echo "Did solution2 in $time seconds.n";

Test 1: Did solution1 in 0.19701099395752 seconds. Did solution2 in 0.38502216339111 seconds.

Test 2: Did solution1 in 0.1990110874176 seconds. Did solution2 in 0.37402105331421 seconds.

Test 3: Did solution1 in 0.19801092147827 seconds. Did solution2 in 0.37002205848694 seconds.

Tuesday, September 27, 2022
3

First of all - accept your recent answers.

You can do this with substr function:

$input  = 'aabbccddeeffgghh';
$output = substr($input, 0, 5) . substr($input, -5);
Wednesday, October 5, 2022
 
5

This operator is used to combine strings.

EDIT

Well, to be more specific if a value is not a string, it has to be converted to one. See Converting to a string for a bit more detail.

Unfortunately it's sometimes mis-used to the point that things become harder to read. Here are okay uses:

echo "This is the result of the function: " . myfunction();

Here we're combining the output of a function. This is okay because we don't have a way to do this using the standard inline string syntax. A few ways to improperly use this:

echo "The result is: " . $result;

Here, you have a variable called $result which we can inline in the string instead:

echo "The result is: $result";

Another hard to catch mis-use is this:

echo "The results are: " . $myarray['myvalue'] . " and " . $class->property;

This is a bit tricky if you don't know about the {} escape sequence for inlining variables:

echo "The results are: {$myarray['myvalue']} and {$class->property}";

About the example cited:

$headers = 'From: webmaster@example.com' . "rn" .
    'Reply-To: webmaster@example.com' . "rn" .
    'X-Mailer: PHP/' . phpversion();

This is a bit tricker, because if we don't use the concatenation operator, we might send out a newline by accident, so this forces lines to end in "rn" instead. I would consider this a more unusual case due to restrictions of email headers.

Remember, these concatenation operators break out of the string, making things a bit harder to read, so only use them when necessary.

Saturday, November 12, 2022
3

You can combine tail and head:

$ tail -n +3 file.txt | head -n -4 > file.txt.new && mv file.txt.new file.txt
Monday, September 5, 2022
 
ter0
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :