PHP preg_replace from @ to next space

im trying to create a function to replace from @ to next space but i cant seem to get it to work propely... i got the quote part to work but this is what i got so far:

function test($body) { 
    $find = array( 
        "/\[quote\](.+?)\[\/quote\]/is",
        "/@(.+?)/is" 
);
 $replace = array(
        "<div class=\"quote\">$1</div>",
        "<a href=\"user.php?profile=$1\">$1</a>"
    ); 
    $body = htmlspecialchars($body); 
    $body = preg_replace($find, $replace, $body); 
    return $body;
}

any help would be really appreciated.

example:

[quote]

@lalalala

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras viverra ornare lectus sit amet dignissim. Vestibulum a mi leo. Nunc placerat accumsan elit, nec luctus ante malesuada sed. Quisque at urna non erat suscipit pharetra.

[/quote]

Morbi massa mauris, consequat vitae sem eu, maximus posuere ligula. Fusce pretium ultricies lectus sit amet bibendum. Aliquam nec dolor urna.

expected output:

<div class="quote"> 

<a href="user.php?profile=lalalala">lalalala</a>            

 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras viverra
 ornare lectus sit amet dignissim. Vestibulum a mi leo. Nunc placerat
 accumsan elit, nec luctus ante malesuada sed. Quisque at urna non erat
 suscipit pharetra. 

</div>

 Morbi massa mauris, consequat vitae sem eu, maximus posuere ligula.
 Fusce pretium ultricies lectus sit amet bibendum. Aliquam nec dolor
 urna.

Upvotes: 0

Views: 136

Answers (3)

mega6382
mega6382

Reputation: 9396

Try the following regex for the second one:

"/@(.+?)\s/is" 

Here is a demo. This will select everything in the text until a space. OR better yet:

"/@(\w+)/is" 

This will only select alphanumeric characters and underscore like [A-Za-z0-9_]. Demo

Upvotes: 0

Andrzej S.
Andrzej S.

Reputation: 472

Try to use \S (not-space character): /@(\S+)/is

Upvotes: 1

ademilsonfp
ademilsonfp

Reputation: 26

Try to replace the regular expression "/@(.+?)/is" with "/@\S+/".

Upvotes: 1

Related Questions