Hi,

I am trying to build a Twiiter website widget using the Twitter API, that will display a list of recent tweets.

I am stuck on one piece which is the display of the URLs in the tweet.

I am using preg_replace to look through the text and find a match and replace etc.. as below:-

$text = preg_replace('/(https{0,1}:\/\/[\w\-\.\/#?&=]*)/', '<span class="tweet_links_intext"><a href="$1" target="_blank">$1</a></span>', $text);

This works perfectly correctly but with one exception, the links are now displayed with the preceeding http://

How can I get the links to be displayed and be clickable without the preceeding http://

I have tried changing the above code around by replacing the $1 between the anchor tags with the actual entity tag from the Twitter API i.e.

status->user->display_url

but this does not work at all.

Any help would be appreciated.

Kind regards..,

MT

Dani AI

Generated

Good catch by — capturing a subpattern for the visible part is a lightweight way to hide the leading scheme while keeping the link clickable. For a more robust, long‑term solution prefer the Tweet JSON entities: each Tweet includes an entities->urls array with url, expanded_url and display_url, so you can replace Twitter's short t.co token with the exact expanded href and a user‑friendly label instead of parsing the text with brittle regexes. (developer.x.com)

When you have the entities available from the API, replace the short URL tokens directly. Example pattern (uses proper escaping and rel attributes for safety):

foreach ($tweet->entities->urls as $u) {
    $href  = htmlspecialchars($u->expanded_url, ENT_QUOTES, 'UTF-8');
    $label = htmlspecialchars($u->display_url, ENT_QUOTES, 'UTF-8');
    $text = str_replace($u->url,
        '<span class="tweet_links_intext"><a href="'.$href.'" target="_blank" rel="noopener noreferrer">'.$label.'</a></span>',
        $text
    );
}

This keeps href as the full expanded URL while showing the shorter display_url. Always escape values with htmlspecialchars to prevent XSS when inserting into HTML. (php.net)

If you must operate on plain tweet text (no entities), use preg_replace_callback to build the anchor in PHP — it lets you keep the full URL in href but strip http:///https:// from the visible label, trim trailing punctuation, and optionally shorten long labels:

$text = preg_replace_callback('#\bhttps?://[^\s<>()\[\]{}]+#i', function($m) {
    $url = $m[0];
    $display = preg_replace('#^https?://#i', '', $url);
    $display = rtrim($display, '.,:;!?');
    $href = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
    $label = htmlspecialchars((strlen($display)>60?substr($display,0,57).'...':$display), ENT_QUOTES, 'UTF-8');
    return '<span class="tweet_links_intext"><a href="'.$href.'" target="_blank" rel="noopener noreferrer">'.$label.'</a></span>';
}, $text);

Use preg_replace_callback for control and safety when you must rely on regex. (php.net)

Final notes: prefer the API entities approach whenever possible (it avoids t.co shortener pitfalls), always HTML‑escape output, and add rel="noopener noreferrer" to _blank links for security/privacy. (developer.x.com)

Recommended Answers

All 4 Replies

How about this

$text = preg_replace('/(https{0,1}:\/\/([\w\-\.\/#?&=]*))/', '<span class="tweet_links_intext"><a href="$1" target="_blank">$2</a></span>', $text);

How about this

$text = preg_replace('/(https{0,1}:\/\/([\w\-\.\/#?&=]*))/', '<span class="tweet_links_intext"><a href="$1" target="_blank">$2</a></span>', $text);

Thanks for that, it certainly done the trick.

For my future reference, why & what is the difference between the $1 & $2?

How does using $2 remove the htt:// part from the URL?

Kind regards..,

MT

I've placed an additional subpattern in the matching pattern which matches only the part of the link after the http://
It basically matches like this: ([url]http://(www.website.com[/url])) which sets the following back-references for the replacement

$1 = [url]
$2 = [url]www.website.com[/url]
commented: spot on, thx +3

I've placed an additional subpattern in the matching pattern which matches only the part of the link after the http://
It basically matches like this: ([url]http://(www.website.com[/url])) which sets the following back-references for the replacement

$1 = [url]
$2 = [url]www.website.com[/url]

Thanks for the info

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.