如何在不使用HTML标记的情况下查找字符串中的最后一个空格(正则表达式PHP)

如何在不使用HTML标记的情况下查找字符串中的最后一个空格(正则表达式PHP)

问题描述:

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?

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 );
}