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 8616915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:43:56+00:00 2026-06-12T05:43:56+00:00

I have the following text: This is a test text. Test, comma instead of

  • 0

I have the following text:

“This is a test text. Test, comma instead of space.”

I iterate through each word and want to replace each word to a distinct link. Let’s say

<a href="wordToReplace">wordToReplace</a>

My problem is that consecutive matches of the word “test” (to use the above example) replace the href and anchor text so I’m left with links inside links which is not good at all.

(to give a base idea of my problem this is what I’m left with. It has some additional markup.)

This is a<a href="/index.php?r<a href="/index.php?r=texts/addWord"
class="wordLink info label" id="yt2">text</a>/addWord" class="wordLink
info label" id="yt1">test</a>text.<a href="/index.php?r=texts/addWord"
class="wordLink info label" id="yt3">Test</a><a
href="/index.php?r=texts/addWord" class="wordLink info label"
id="yt4">comma</a>instead of<a href="/index.php?r=texts/addWord"
class="wordLink info label" id="yt6">space</a>

I’m trying

preg_replace("/[^\">]".$word."[^\"<]/", $link, $text->text);

but I don’t think I’m on the right track at all.

Thanks for the time

  • 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-12T05:43:58+00:00Added an answer on June 12, 2026 at 5:43 am

    From your one line of code, I’m assuming that you have that line in a loop which iterates through all the $words you want to replace, which causes the problem.

    What you need to do is put all those replacements into only one preg_replace call. For that, regexes provide alternatives. So say, your list of words consisted of test, text and this. Then you could do:

    preg_replace('/(test|text|this)/', $link, $text->text);
    

    And if you have all your words in an array $words then you can generate the regex simply with:

    $wordList = implode('|', $words);
    preg_replace('/('.$wordList.')/', $link, $text->text);
    

    You might want to add an i at the very end of your regex, if your checks are supposed to be case-insenstive.

    In case some of your words are parts of other words (e.g. you want to replace text and texture), you could check for word boundaries:

    preg_replace('/\b('.$wordList.')\b/', $link, $text->text);
    

    Is this what you are looking for?

    EDIT: If your $link was pre-generated for every $word in your loop before, you can now replace that word with $1. If your $link was something like

    $link = '<a href="'.$word.'">'.$word.'</a>';
    

    you can now simply use

    $link = '<a href="$1">$1</a>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following: $(span.findme).each(function() { $(<li>).text($(this).text()).appendTo(ul); }); How can I, using the above,
I have the following XML: <questions> <question text=This is a test question?> <answer value=Yes>
I have the following text contained within a field in my DB: [quote:5a7b87febe=mr smith]This
I have this following jquery text fly-in animation.Here is my code before I explain
I have the following function: if ($(this).find('#sel').length == 0) { var before = $(this).text();
I have the following tweets table: tweet_id user_id text --------------------------------------------------- 1 2 this is
I have the following text; country=france name=jean country=germany name=michael country=england name=jack I want it
I have the following template. I want to display the categories with a comma
I have the following code: InputStream reportFile = MyPage.this.getClass().getResourceAsStream(test.jrxml); HashMap<String, String> parameters = new
I have this test.txt file with the following content: @echo off wget -q http://subs.ro/get/21518

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.