-3

I'm searching for a regex to find the last occurence of a space in my text but I'don't want to find a space from in a HTML tag.

"This is a string with <strong>some</strong> html in it"

The space that the regexp should find is the one between in and it. That regex is not so difficult. The same regex would also work here:

"This is a string with <strong>some</strong> html in <a href="">the end</a>"

The space would be now in the HTML between the and end (ok!)

But when my string is:

"This is a string with <strong>some</strong> html in the <a href="">end</a>"

Then the space should be between the and <a and not between <a and href="">end<a>.

Anyone some idea?

1 Answers1

3

Updating this answer since more information about requirements came to light.

A combination of strip_tags(), strrpos(), substr() functions will do the trick. Use the strip_tags() function to clean out the HTML first. You'll then be left with the text and can explode it to find the last word, and then use strrpos() to find the position of that word in the original text.

$stringToken = explode( ' ', strip_tags( $str ) );
// Find the second-to-last word in the string.
$word = $stringToken[ count( $string ) - 2 ];

// Use $word to find its position within the original HTML-encoded string.
$wordPosition = strrpos( $str, $word );

if( $wordPosition !== false )
{
    $finalSpace = strpos( $str, ' ', $wordPosition );
    $lastSpacePrefix = substr( $str, 0, $finalSpace );
    $lastSpaceSuffix = substr( $str, $finalSpace + 1 );
    $newStr = sprintf( "%s%s%s", $lastSpacePrefix, $finalSpaceSub, $lastSpaceSuffix );
}
Thomas
  • 1,402
  • 1
  • 11
  • 14
  • I'm searching the last occurence of the space to replace it with something so stripping the HTML is not possible. – Jonas De Smet Aug 23 '12 at 14:45
  • In that case I would recommend using the DOMDocument object built into PHP. Trying to use REGEX to parse HTML is a losing proposition every time. A great discussion of this can be found here: http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php – Thomas Aug 23 '12 at 14:59
  • Using the DOMDocument is not an option, this is for a class, I don't want much dependencies. – Jonas De Smet Aug 23 '12 at 15:30