0

I'm using this preg_replace to find links in tweet text and it dutifully returns an HTML link but I wonder if it's possible to, in the same line, lose the http:// from the link text. For example:

$output .= preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a target="blank" rel="nofollow" href="$1" target="_blank">$1</a>', $status->text);

might return:

<a target="blank" rel="nofollow" href="https://kanyewest.com/">https://kanyewest.com</a>

but I'd really like it to return:

<a target="blank" rel="nofollow" href="https://kanyewest.com/">kanyewest.com</a>
5
  • Take a look at this stackoverflow.com/a/32187000/3885509 Commented Oct 13, 2016 at 13:33
  • That function works great, @Charlotte, except it strips the second part of the link where there is a '/' later on. For example 't.co/twJ6g0rqtz' comes back as just 't.co' in the link text. Commented Oct 13, 2016 at 13:39
  • Replace $4 with $4$5 inside the replace argument. Commented Oct 13, 2016 at 13:40
  • Sorry, yes. Thanks @Charlotte. I just re-read the answer and found that. Awesome, perfect! Commented Oct 13, 2016 at 13:40
  • Possible duplicate of Show only the domain name when replacing a URL with hyperlink Commented Oct 13, 2016 at 13:41

1 Answer 1

-1

You could just use a Substring. http://php.net/manual/en/function.substr.php

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

Comments

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.