Forumlar / Craftwork / Server-side / preg_replace questions

I'm going mad atm, trying to figure it out?

fade2k
#1 2012-06-18 15:56

I'm my oddball quest to learn PHP, I've been working on simple applications (what better way to learn right?). So I find myself using preg_replace to convert usernames into links in posts using preg_replace. My hangup so far has been the whole regex .. I get it .. sort-of.

$var = preg_replace('/[!]+('.$value.')/',$shoutLink,$var); // $var = a post

The shoutlink is just a link to the members profile. Anyway, it works if the !username is on the first line of the input but when there are new lines without a space afterwords and another !username -- both fail to be replaced with the link. What gives? Does preg_replace see new lines as part of the input (\n!username) or does it ignore it?

LoL thanks ahead of time.

- Be Kind, Please Rewind -
GHengeveld
#2 2012-06-18 20:59

I think what you're looking for is something like this:

$var = preg_replace('/!([^\s])/', '<a href="index.php?e=users&m=details&u=$1">$1</a>', $var);

A helpful tool to check your regular expressions is here: http://gskinner.com/RegExr/

By the way, don't you think the @ sign is more appropriate (considering Twitter's usage of this).

fade2k
#3 2012-06-19 21:56

Thank you & Thanks for the link!

Your probably right about the @ sign -- Got use to a pesky !bot =0D

I'm slowly getting ahang of this stuff =0)

- Be Kind, Please Rewind -