Norman
Norman

Reputation: 6365

PHP trim and return a string from its right

I'm trying to take a string that's output from MySql like this (MySql outputs X characters):

$str = 'Buddy you're a boy make a big noise Playin in the stre';

and trying to start from the right side, trim whatever is there up till the first space. Sounded simple when I got down to it, but now, it has my brain and fingers in knots.

The output I'm tying to achieve is simple:

$str = 'Buddy you're a boy make a big noise Playin in the';

Notice, that characters starting from the right, till the first space, are removed.

Can you help?

My Fiddle

$str = 'Buddy you\'re a boy make a big noise Playin in the stre';

//echo rtrim($str,' ');

Upvotes: 0

Views: 99

Answers (6)

mickmackusa
mickmackusa

Reputation: 47904

As a matter of regex performance comparison, the regex engine can move faster through the string when it can perform greedy matching with minimal backtracking.

  1. / +[^ ]*$/ uses 68 steps. (@raina77ow)
  2. /(?:[^ ]+\K )+.*/ uses 56 steps. (@mickmackusa)
  3. /(?:\K [^ ]*)+/ uses 48 steps. (@mickmackusa)
  4. \s+\S*$ uses 34 steps. (@ChrisBornhoft and @RyanKempt)
  5. /.*\K .*/ uses just 15 steps. (@mickmackusa)

Based on these comparisons, I recommend greedily matching any characters, then restarting the fullstring match before matching the last occurring space, then matching zero or more characters until the end of the string.

Code: (Demo)

$string = "Buddy you're a boy make a big noise Playin in the stre";
var_export(
    preg_replace('/.*\K .*/', '', $string)
);

Output:

'Buddy you\'re a boy make a big noise Playin in the'

Upvotes: 0

raina77ow
raina77ow

Reputation: 106375

It's a useful idiom to remember on its own: to remove all the characters preceding a specific one from the right side of the string (including that special character), use the following:

$trimmed = substr($str, 0, strrpos($str, ' '));

... where ' ' is that special character.

Demo

If you don't know, however, whether or not the character is present, you'd check the result of sttrrpos first:

$last_space_index = strrpos($str, ' ');
$trimmed = $last_space_index !== false
  ? substr($str, 0, $last_space_index)
  : $str;

And if there can be more than one character that you need to trim, like in 'hello there test' line, just rtrim the result:

$trimmed = rtrim(substr($str, 0, strrpos($str, ' ')), ' ');

In this case, however, a regex-based solution looks more appropriate:

$trimmed = preg_replace('/ +[^ ]*$/', '', $str);

Upvotes: 4

Ryan Kempt
Ryan Kempt

Reputation: 4209

There's a hundred ways to do this, here are some options:

array_pop'ing the last word off an array we create from explode:

$arr = explode(" ", $str);
$fixed_arr = array_pop($arr);
$result = implode(" ", $arr);

Using regular expressions:

$result = preg_replace('/\s+\S*$/', '', $str);

and using strrpos and substr:

$spacePos = strrpos($str, ' ');
$result = substr($str, 0, $spacePos);

Upvotes: 1

Chris Bornhoft
Chris Bornhoft

Reputation: 4291

I think your best option would be a regex replace:

preg_replace('/\s+\S*$/', '', $str);

which outputs Buddy you're a boy make a big noise Playin in the

And the Fiddle

Upvotes: 2

leseulsteve
leseulsteve

Reputation: 335

it's probably easier to do it with regex, but I'm sooo bad with that! You shoud try this:

// Get all the words in an array
$strArray = explode(" ", $str);

// Remove the last word.
array_pop($strArray);

// Get it back into a sentence
$newString = implode(" ", $strArray);

Upvotes: 1

Jack
Jack

Reputation: 302

In mysql use

left(field,length)

to output only the strlen first digits

right(field,length) having opposite effects

otherwise use substr($string,0,$length) or regex in php

Upvotes: 0

Related Questions