views:

124

answers:

5
$variable = 'put returns between paragraphs';

Value of this variable everytime changes.

How to add some text before the last word?


Like, if we want to add 'and', the result should be (for this example):

$variable = 'put returns between and paragraphs';
+1  A: 
1) reverse your string
2) find the first whitespace in the string.
3) chop off the remainder of the string.
4) reverse that, append your text
5) reverse and add back on the first portion of the string from step 3, including extra whitespace as needed.
Zak
reversing that many times is completely unnessecary
GSto
of course it is unnecessary, but it is simple, and this simple question clearly needs a simple, understandable answer in plain English (not code) to walk through the logical process of how to solve a problem.
Zak
If you didn't have `strrpos`, this algorithm seems quite reasonable.
erisco
+1  A: 
$addition = 'and';
$variable = 'put returns between paragraphs';
$new_variable = preg_replace('/ ([^ ]+)$/', ' ' . $addition . ' $1', $variable);
Tim Cooper
Consider replacing the `*` with `+` in the pattern.
jensgram
@jensgram Thanks for catching that.
Tim Cooper
@Tim Cooper NP.
jensgram
+1  A: 

You can use preg_replace():

$add = 'and';
$variable = 'put returns between paragraphs';    
echo preg_replace("~\W\w+\s*$~", ' ' . $add . '\\0', $variable);

Prints:

put returns between and paragraphs

This will ignore trailing whitespaces, something @jensgram's solution doesn't. (eg: it will break if your string is $variable = 'put returns between paragraphs '. Of course you can use trim(), but why bother to waste more memory and call another function when you can do it with regex ? :-)

NullUserException
I can't attribute the source, but I once heard this great quote "I had a problem and decided to use regexes. Now I have two problems"
Zak
how to add some html instead of 'and' in your solution?
Happy
@Zak It's not a problem if you understand regex, and know what it can and cannot do.
NullUserException
seems regex is some sort of magic
Happy
@Happy see updated answer; change the `$add` variable.
NullUserException
+6  A: 

You can find the last whitespace using the strrpos() function:

$variable = 'put returns between paragraphs';
$lastSpace = strrpos($variable, ' '); // 19

Then, take the two substrings (before and after the last whitespace) and wrap around the 'and':

$before = substr(0, $lastSpace); // 'put returns between'
$after = substr($lastSpace); // ' paragraphs' (note the leading whitespace)
$result = $before . ' and' . $after;

EDIT
Although nobody wants to mess with substring indexes this is a very basic task for which PHP ships with useful functions (specificly strrpos() and substr()). Hence, there's no need to juggle arrays, reversed strings or regexes - but you can, of course :)

jensgram
great answer jensgram.
Zak
@NullUserException You're right about a possible trailing whitespace (`trim()` might be the solution). In terms of which solution is "cleaner" it's highly subjective. The above is easy to comment (and thus, easy to understand) whereas I myself find regex solutions neat, too.
jensgram
I find my regex solution cleaner than this. Besides you can tweak it to use different separators, or ignore trailing white spaces (like mine does). This will break if your string is `'put returns between paragraphs '` (with a trailing whitespace)
NullUserException
@jensgram Yeah, I know you could put `trim()` in there. The downside is that now you just added yourself more overhead and more memory usage.
NullUserException
@NullUserException Overhead? Yes, I guess. Memory usage? I think it's negligible in almost any "normal" situation. Besides, we don't even now whether trailing whitespaces is a possible issue.
jensgram
@jensgram "In terms of which solution is "cleaner" it's highly subjective". That's why I said " *I* find my regex solution cleaner" ;-)
NullUserException
+1  A: 

another option

  <?php
  $v = 'put returns between paragraphs';
  $a = explode(" ", $v);
  $item = "and";
  array_splice($a, -1, 0, $item);
  echo  implode(" ",$a);
  ?>
mcgrailm