tags:

views:

31

answers:

1
$text = 'something here *ls67 another thing'; // match
$text2 = 'something here *ls67. another thing'; // match
$text3 = 'something here  another thing *ls67.'; // match
$text3 = 'something here  another thing *ls67'; // doesn't match (and i need to match this)

$pattern = '|^(.*)?(\*[0-9a-zA-Z_-]{3,15})([^0-9a-zA-Z_-])+(.*)?$|i';

I've tried this pattern but it generates an error:

$pattern = '|^(.*)?(\*[0-9a-zA-Z_-]{3,15})([^0-9a-zA-Z_-]|$)+(.*)?$|i'; // Unknown modifier '$'
// and
$pattern = '|^(.*)?(\*[0-9a-zA-Z_-]{3,15})(([^0-9a-zA-Z_-])+|$)(.*)?$|i'; // same error
+2  A: 

Since you're using | as your delimiter, you need to escape any |'s that appear in the pattern (or you could change it to a character that doesn't appear in your pattern, such as #. Since you introduced unescaped |'s in your second block of code, PCRE tries to take the characters following (staring with $) as modifiers, hence the error.

This should work:

$pattern = '#^(.*)?(\*[0-9a-zA-Z_-]{3,15})([^0-9a-zA-Z_-]+(.*)?|$)#i';
Daniel Vandersluis
@Daniel Vandersluis: Thanks man!
Cesar