11

I have a little issue I'm trying to find a solution for.

Basically, imagine you have the following string:

    $string = 'Hello I am a string';

And you'd like it to end with something like the folowing:

    $string = 'Hello I am a string';

Simply, replacing the last occurrence of a space, with a non-breaking space.

I'm doing this because I don't want the last word in a heading to be on its own. Simply because when it comes to headings:

 Hello I am a
 string

Doesn't look as good as

 Hello I am
 a string

How does one do such a thing?

willdanceforfun
  • 10,674
  • 31
  • 81
  • 121
  • possible duplicate of http://stackoverflow.com/questions/3835636/php-replace-last-occurence-of-a-string-in-a-string – Shakti Singh Feb 28 '11 at 07:01
  • 2
    That last word is called an [orphan](http://en.wikipedia.org/wiki/Widows_and_orphans). – alex Feb 28 '11 at 07:27
  • Correct! ppl in print land are familiar with the term.. i didn't think it would be too recognised here. But yeah, can't have orphans... they are sad. – willdanceforfun Feb 28 '11 at 07:42

5 Answers5

23

Code from this example will do the trick:

// $subject is the original string
// $search is the thing you want to replace
// $replace is what you want to replace it with

substr_replace($subject, $replace, strrpos($subject, $search), strlen($search));
poundifdef
  • 17,354
  • 21
  • 84
  • 128
  • It will only work as expected, if `$subject` actually contains at least one occurence of `$search`. Otherwise `strrpos($subject, $search)` will return `false`, that will be interpreted as `0` and the first `strlen($search)` characters of `$subject` will replaced by `$replace`. Ergo: the function call needs to be wrapped with an `if`: `if(strrpos($subject, $search) !== false) { ... }` – automatix May 01 '13 at 18:32
  • The link is now broken if I'm not mistaken. – Naguib Ihab Jul 02 '15 at 05:43
8
echo preg_replace('/\s(\S*)$/', ' $1', 'Hello I am a string');

Output

Hello I am a string

CodePad.

\s matches whitespace characters. To match a space explictly, put one in (and change \S to [^ ]).

alex
  • 460,746
  • 196
  • 858
  • 974
  • If input is not trimmed, I would use `/\s(\S+)\s*$/` as a pattern, as `'/\s(\S*)$/'` would change `"some string "` into `"some string "` rather than `"some string"`. It might also be good to handle multiple spaces, i.e., `some  string` (2 spaces between words) should be converted to `some string` rather than `some  string`, so pattern would be `/\s+(\S+)\s*$/`. – binaryLV Feb 28 '11 at 08:28
1

as per pounndifdef's answer, however i needed to decode the   HTML entity like so:

substr_replace($subject, html_entity_decode($replace), strrpos($subject, $search), strlen($search));

also worked using alex's answer:

preg_replace('/\s(\S*)$/', html_entity_decode(' ').'$1', 'Hello I am a string');
camslice
  • 451
  • 5
  • 5
1

This would do the trick:

$string = preg_replace('/([\s\S]+)\s(\w)$/','$1 $2',$string);
Shad
  • 14,384
  • 2
  • 20
  • 34
0

Use str_replace() like normal, but reverse the string first. Then, reverse it back.

Richard Pianka
  • 3,257
  • 2
  • 27
  • 34