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

  • SEARCH
  • Home
  • 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 7709741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:56:18+00:00 2026-06-01T00:56:18+00:00

I am trying to convert specific keywords in text, which are stored in array,

  • 0

I am trying to convert specific keywords in text, which are stored in array, to the links.

Example text:

$text='This text contains many keywords, but also formated <a href="#keywords" title="keywords">keywords</a>.'

So now I want to convert the word keywords to the <a href="#keywords" title="keywords">#keywords</a>.

I used the very simple preg_replace function

preg_replace('/keywords/i',' <a href="#keywords">keywords</a> ',$text);

but obviously it converts to link also the string already formatted as a link, so I get a messy html like:

$text='This text contains many <a href="#keywords" title="keywords">keywords</a>, but also formated <a href="#<a href="#keywords" title="keywords">keywords</a>" title="<a href="#keywords" title="keywords">keywords</a>"><a href="#keywords" title="keywords">keywords</a></a>.'

Expected result:

$text='This text contains many <a href="#keywords" title="keywords">keywords</a>, but also formated <a href="#keywords" title="keywords">keywords</a>.'

Any suggestions?
THX

EDIT

We are one step from the perfect function, but still not working well in this case:

$text='This text contains many keywords, but also formated 
       <a href="http://www.keywords.com/keywords" title="keywords">keywords</a>.'

In this case it replaces also the word keywords in the href, so we again get the messy code like

 <a href="http://www.<a href="http://www.keywords.com/keywords" title="keywords">keywords</a>.com/<a href="http://www.keywords.com/keywords" title="keywords">keywords</a>" title="keywords">keywords</a>
  • 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-01T00:56:19+00:00Added an answer on June 1, 2026 at 12:56 am

    I’m not great with regular expressions, but maybe this one will work:

    /[^#>"]keywords/i
    

    What I think it will do is ignore any instances of #keywords, >keywords, and "keywords and find the rest.


    EDIT:

    After testing it out, it looks like that replaces the space before the word as well, and doesn’t work if keywords is the beginning of the string. It also didn’t preserve original capitalization. I have tested this one, and it works perfectly for me:

    $string = "Keywords and keywords, plus some more keywords with the original <a href=\"#keywords\" title=\"keywords\">keywords</a>.";
    $string = preg_replace("/(?<![#>\"])keywords/i", "<a href=\"#keywords\">$0</a>", $string);
    echo $string;
    

    The first three are replaced, preserving the original capitalization, and the last one is left untouched. This one uses a negative lookbehind and backreferences.


    EDIT 2:

    OP edited question. With the new example provided, the following regex will work:

    $string = 'This text contains many keywords, but also formated <a href="http://www.keywords.com/keywords" title="keywords">keywords</a>.';
    $string = preg_replace("/(?<![#>\".\/])keywords/i", "<a href=\"http://www.keywords.com/keywords\" title=\"keywords\">$0</a>", $string);
    echo $string;
    
    // outputs: This text contains many <a href="http://www.keywords.com/keywords" title="keywords">keywords</a>, but also formated <a href="http://www.keywords.com/keywords" title="keywords">keywords</a>.
    

    This will replace all instances of keywords that are not preceded by #, >, ", ., or /.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to convert the following but am having difficulty. Can anyone see where I'm
Trying to convert this c code into MIPS and run it in SPIM. int
Trying to convert int arrays to string arrays in numpy In [66]: a=array([0,33,4444522]) In
Im trying to convert unixtime to date but the results im getting are wrong
I'm trying to execute a program (convert from ImageMagick, to be specific) whose parent
This is a very odd, and quite specific question. Ultimately I am trying to
I've got an infopath form for which I am trying to convert the submit
I am trying to convert a string into uppercase using a specific character encoding,
I'm trying to convert the following xpath to find the same element, but using
I am trying to convert my shell scripts into python code, but I am

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.