Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9099517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:38:17+00:00 2026-06-17T00:38:17+00:00

I have a string that contains text, and in several places there will be

  • 0

I have a string that contains text, and in several places there will be a twitter-style hashtag. I want to find them and create a seperate variable where all of them are seperated by spaces. I also want to convert all of the hashtags in the original string into links. Example:

$string = "Hello. This is a #hashtag and this is yet another #hashtag. This is #another #example."

after function:

$string_f = "Hello this is a <a href='#'>#hashtag</a> and this is yet another <a href='#'>#hashtag</a>. This is <a href='#'>another</a> <a href='#'>example</a>";

$tags = '#hashtag #another #example';
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:38:18+00:00Added an answer on June 17, 2026 at 12:38 am

    To find all of the hash tags, use a regex and preg_match_all(), and do the replacement with preg_replace():

    $regex = '/(#[A-Za-z-]+)/';
    preg_match_all( $regex, $string, $matches);
    $string_f = preg_replace( $regex, "<a href='#'>$1</a>", $string);
    

    Then all of the tags are in an array in $matches[1]:

    $tags_array = $matches[1];
    

    Then, convert that to a space-separated list with implode() and array_unique():

    $tags = implode( ' ', array_unique( $tags_array));
    

    And you’re done. You can see from this demo that $tags and $string_f are:

    "#hashtag #another #example"
    "Hello. This is a <a href='#'>#hashtag</a> and this is yet another <a href='#'>#hashtag</a>. This is <a href='#'>#another</a> <a href='#'>#example</a>."
    

    For other characters in the hashtag (for example, digits), modify the $regex appropriately.

    Edit: However, this can be improved in efficiency if you use preg_replace_callback() and a closure so you only have to execute the regex once, like so:

    $tags_array = array();
    $string_f = preg_replace_callback( '/(#[A-Za-z-]+)/', function( $match) use( &$tags_array) { 
        $tags_array[] = $match[1];
        return "<a href='#'>" . $match[1] . "</a>";
    }, $string);
    $tags = implode( ' ', array_unique( $tags_array));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that contains some text: <p>ProductImage [height:60]</p> and I want to
I have a long string that contains text. I want to extract every word
I have a string of text that contains html, and I need to extract
Suppose I have a string that contains Ü. How would I find all those
I have an XML that contains several flags, some of them are unsigned 32-bit
I have a string that contains well formed xml in it. I want to
I have a string variable Result that contains a string like: <field1>text</field1><field2>text</field2> etc.. I
I have an HTML string that contains text and images, e.g. <p>...</p> <img src=...
I have a string that contains the following text String my_string = hello world.
I have a text file that contains some strings separated by ,. Strings are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.