1

So i have this here

<? $regex = array('/@(\w+)/','/#(\w+)/','/((www|http:\/\/)[^ ]+)/');
    $replace = array(
    '<a href="https://www.twitter.com/$1">@$1</a>',
    '<a href="https://twitter.com/#!/search/%23$1">#$1</a>',
    '<a href="\1">\1</a>'
); ?>

<?= preg_replace($regex,$replace,stripslashes($row['tweet_text']));?>

The first two are suppose to turn anything with @ and # into links. This is obviously for twitter. But the third is supposed to turn anything with http or www into a link. But it seems to be conflicting with the first two messing up the links.

How can i make the third one make http or www links without conflicting the other two?

1 Answer 1

1

This is going to be hacky at best, but you could try this:

<? $regex = array('/((www|http:\/\/)[^ ]+)/', '/(?<!>)@(\w+)/', '/(?<!>)#(\w+)/');
    $replace = array(
    '<a href="\1">\1</a>',
    '<a href="https://www.twitter.com/$1">@$1</a>',
    '<a href="https://twitter.com/#!/search/%23$1">#$1</a>'
); ?>

Turn the order around, fixing the links first, then only replace @ and # handles if they are not preceded by a > (which they would be if they were already inside an anchor tag).

Sign up to request clarification or add additional context in comments.

1 Comment

@soniccool: Basically, yes. The (?<!>) is just an extra precaution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.