I have this regex with the preg_replace function on PHP :

$str=preg_replace(
    '#\b((Hello ).+)#',
    '<a class="lforum" href="$1">$1</a>',
    $str);

It checks all strings that start with Hello and are followed by any kind of chars (at least one char, with repetition).

So for example :

Hello Mark \\ is checked
HelloMark  \\ is not checked

The problem now is that also this string is checked :

Hello Mark Cordi

because white space is a char, anyway.

I don't want this. Or, better, if the string is Hello Mark Cordi, it must replace only Hello Mark.

How can I do this? Thanks

EDIT Problem with newline

My actual function :

echo example(htmlentities($myString, ENT_QUOTES, "UTF-8"));

function example($str) {
    $str=preg_replace(
        '#((Hello )[^ \n]+)#',
        '<a class="lforum" href="$1">$1</a>',
        $str);

    return nl2br($str);     
}

If $myString is :

Hello Mario
Ciao

(notice the newline, so at the end of Hello Mario there is a \n) the output is this :

<a class="lforum" href="Hello Mario<br />">Hello Mario<br /></a><br />Ciao

instead of :

<a class="lforum" href="Hello Mario">Hello Mario</a><br />Ciao

So it add that \n with $1 on replace, and it shouldnt :(

Comments

regular-expressions.info is your friend :)

Written by FallingBullets

Accepted Answer

Use [^ ] (everything but a whitespace) instead of ..

[^abc] means "everything but a, b and c". Here we use it with a single whitspace.

Edit (2):

This is working:

  $str=preg_replace(
        '#(Hello [^\s\n<]+)#',
        '<a class="lforum" href="$1">$1</a>',
        $str);

It is ok for strings like this: Mark<..., Mark\n..., Mark ... (replace ... with what you want).

[^\s\n<] means "everything but spaces (\s), newlines (\n), and <".

Written by boudou
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki