tags:

views:

46

answers:

3
+1  Q: 

PHP Regex Question

Hi,

I am developing an application using PHP but I am new to regular expressions, I could not find a solution to my problem. I want to replace all occurences of #word with a link, i have written a preg_match for this:

$text=preg_replace('~#([\p{L}|\p{N}]+)~u', '<a href="/?aranan=$1">#$1</a>', $text);

The problem is, this regular expression also matches the html character codes like

&#039;

and gives corrupt output. I need to exclude the words starting with &# but i do not know how to do that using regular expressions.

Thanks for your help.

A: 

You would need to add a [A-Za-z] rule in your regular expression statement so that it only limits itself to letters and no numbers.

I will edit with an example later on.

Christopher Richa
I think he wants numbers because he uses \p{L}|\p{N} not just \p{L}
Kamil Szot
hmm yeah I just realized it by rereading your question. But at least you got an answer to your problem.
Christopher Richa
+2  A: 
'~(?<!&)#([\p{L}|\p{N}]+)~u'

That's a negative lookbehind assertion: http://www.php.net/manual/en/regexp.reference.assertions.php

Matches # only if not preceded by &

Kamil Szot
Thanks, that is exactly what I was looking for.
The vertical bar (`|`) is not necessary within the character class (`[...]`).
salathe
+1  A: 

http://gskinner.com/RegExr/

use this online regular expression constructor. They have explanation for every flag you may want to use.. and you will see highlighted matches in example text.

and yes use [a-zA-Z]

holms
I need letters from other languages, that's why I am not using [a-zA-Z].
there's some mbstring regexp functions too they help to use nonlatin matches.. or stmng like this=)
holms