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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:16:01+00:00 2026-05-26T07:16:01+00:00

I’ve been trying to do a regex to match and replace the occurrences of

  • 0

I’ve been trying to do a regex to match and replace the occurrences of a keyword on a portion of HTML:

  1. i want to match keyword and <strong>keyword</strong>
  2. but <a href="someurl.html" target="_blank">keyword</a> and <a href="someur2.html">already linked keyword </a> should NOT be matched

I’m only interested in matching (and replacing) the keyword on the 1st line.

The reason I want this is to replace keyword with <a href="dictionary.php?k=keyword">keyword</s>, but ONLY if keyword it’s not already inside an <a> tag.

Any help will be much appreciated!

  • 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-05-26T07:16:02+00:00Added an answer on May 26, 2026 at 7:16 am

    I managed to do what I wanted (without using Regex) by:

    • parsing each character of my string
    • removing all <a> tags (copying them to a temporary array and keeping a placeholder on the string)
    • str_replace the new string in order to replace all the keywords
    • repopulating the placeholders by it’s original <a> tags

    Here’s the code I used, in case someone else needs it:

    $str = <<<STRA
    Moses supposes his toeses are roses,
    but <a href="original-moses1.html">Moses</a> supposes erroneously;
    for nobody's toeses are posies of roses,
    as Moses supposes his toeses to be.
    Ganda <span class="cenas"><a href="original-moses2.html" target="_blank">Moses</a></span>!
    STRA;
    
    $arr1 = str_split($str);
    
    $arr_links = array();
    $phrase_holder = '';
    $current_a = 0;
    $goto_arr_links = false;
    $close_a = false;
    
    foreach($arr1 as $k => $v)
    {
        if ($close_a == true)
        {
            if ($v == '>') {
                $close_a = false;
            } 
            continue;
        }
    
        if ($goto_arr_links == true)
        {
            $arr_links[$current_a] .= $v;
        }
    
        if ($v == '<' && $arr1[$k+1] == 'a') { /* <a */
            // keep collecting every char until </a>
            $arr_links[$current_a] .= $v;
            $goto_arr_links = true;
        } elseif ($v == '<' && $arr1[$k+1] == '/' && $arr1[$k+2] == 'a' && $arr1[$k+3] == '>' ) { /* </a> */
            $arr_links[$current_a] .= "/a>";
    
            $goto_arr_links = false;
            $close_a = true;
            $phrase_holder .= "{%$current_a%}"; /* put a parameter holder on the phrase */
            $current_a++;
        }    
        elseif ($goto_arr_links == false) {
            $phrase_holder .= $v;
        }
    }
    
    echo "Links Array:\n";
    print_r($arr_links);
    echo "\n\n\nPhrase Holder:\n";
    echo $phrase_holder;
    echo "\n\n\n(pre) Final Phrase (with my keyword replaced):\n";
    $final_phrase = str_replace("Moses", "<a href=\"novo-mega-link.php\">Moses</a>", $phrase_holder);
    echo $final_phrase;
    echo "\n\n\nFinal Phrase:\n";
    foreach($arr_links as $k => $v)
    {
        $final_phrase = str_replace("{%$k%}", $v, $final_phrase);
    }
    echo $final_phrase;
    

    The output:

    Links Array:

    Array
    (
        [0] => <a href="original-moses1.html">Moses</a>
        [1] => <a href="original-moses2.html" target="_blank">Moses</a>
    )
    

    Phrase Holder:

    Moses supposes his toeses are roses,
    but {%0%} supposes erroneously;
    for nobody's toeses are posies of roses,
    as Moses supposes his toeses to be.
    Ganda <span class="cenas">{%1%}</span>!
    

    (pre) Final Phrase (with my keyword replaced):

    <a href="novo-mega-link.php">Moses</a> supposes his toeses are roses,
    but {%0%} supposes erroneously;
    for nobody's toeses are posies of roses,
    as <a href="novo-mega-link.php">Moses</a> supposes his toeses to be.
    Ganda <span class="cenas">{%1%}</span>!
    

    Final Phrase:

    <a href="novo-mega-link.php">Moses</a> supposes his toeses are roses,
    but <a href="original-moses1.html">Moses</a> supposes erroneously;
    for nobody's toeses are posies of roses,
    as <a href="novo-mega-link.php">Moses</a> supposes his toeses to be.
    Ganda <span class="cenas"><a href="original-moses2.html" target="_blank">Moses</a></span>!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a jquery bug and I've been looking for hours now, I can't
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.