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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:29:09+00:00 2026-05-28T16:29:09+00:00

A better example would be this: $string = That is a very nice ford

  • 0

A better example would be this:

$string = "That is a very nice ford mustang, if only every other ford was quite as nice as this honda";

I want to replace the car names with a link for manufacturer and model if they match, or just manufacturer, but if there is a make and model it puts links within links if you use str replace for example….

$remove = array("ford mustang","ford","honda");
$replaceWith = array("<a href='fordID'>ford</a>","<a href='fordmustangID'>ford mustang</a>","<a href='hondaID'>honda</a>");

This gives the result:

"That is a very nice <a href='<a href='fordmustangID'>ford mustang</a>ID'><a href='fordmustangID'>ford mustang</a></a>, if only every other <a href='fordmustangID'>ford mustang</a> was quite as nice as this <a href='hondaID'>honda</a>"

I only want it to create a hyperlink if there isn’t one already like this:

  "That is a very nice <a href='fordmustangID'>ford mustang</a>, if only every other <a href='fordID'>ford</a> was quite as nice as this <a href='hondaID'>honda</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-05-28T16:29:10+00:00Added an answer on May 28, 2026 at 4:29 pm

    edit:

    Took me a good while, but here’s what I came up with:

    function replaceLinks($replacements, $string){
        foreach($replacements as $key=>$val){
            $key=strtolower((string)$key);
            $newReplacements[$key]=array();
            $newReplacements[$key]['id']=$val;
            //strings to make sure the search isn't in front of
            $newReplacements[$key]['behinds']=array();
            //strings to make sure the search isn't behind
            $newReplacements[$key]['aheads']=array();
            //check for other searches this is a substring of
            foreach($replacements as $key2=>$val2){
                $key2=(string)$key2;
                /* 
                //debugging
                $b = ($key=='11 22'&&$key2=='11 22 33');
                if($b){
                    l('strlen $key2: '.strlen($key2));
                    l('strlen $key: '.strlen($key));
                    l('strpos: '.(strpos($key2,$key)));
    
                }
                */
                //the second search is longer and the first is a substring of it
                if(strlen($key2)>strlen($key) && ($pos=strpos($key2,$key))!==false){
                    //the first search isn't at the start of the second search ('the ford' and 'ford')
                    if($pos!=0){
                        $newReplacements[$key]['behinds'][]=substr($key2,0,$pos);
                    }
                    //it's not at the end ('ford' and 'fords')
                    if(($end=$pos+strlen($key))!=strlen($key2)){
                        $newReplacements[$key]['aheads'][]=substr($key2,$end);
                    }
                }
            }
        }
        foreach($newReplacements as $key=>$item){
            //negative lookbehind for words or >
            $tmp="/(?<![\w>=])";
            //negative lookbehinds for the beginnings of other searches that this search is a subtring of
            foreach($item['behinds'] as $b){
                $tmp.="(?<!$b)";
            }
            //the actual search
            $tmp.="($key)";
            //negative lookaheads for ends of other searches that this is a substring of.
            foreach($item['aheads'] as $a){
                $tmp.="(?!$a)";
            }
            //case insensitive
            $tmp.='/ie';
            $replacementMatches[]=$tmp;
        }
        return preg_replace($replacementMatches,'"<a href=\"".$newReplacements[strtolower("$1")]["id"]."\">$1</a>"' ,$string);
    
    }
    

    Pass it an array such as the one you were talking about:

    $replaceWith = array('ford mustang'=>123,'ford'=>42,'honda'=>324);
    

    and a string:

    $string = "That is a very nice ford mustang, if only every other ford was quite as nice as this honda";
    
    echo replaceLinks($replaceWith,$string);
    

    It gives precedence to larger string keys, so if you have ford and ford mustang, it will replace ford mustang with the link.


    Not very practical, but might work.

    $string = "That is a very nice ford mustang, if only every other ford was quite as nice as this honda";
    $remove = array("/(?<![\w>])ford mustang(?![\w<])/",'/(?<![>\w])ford(?! mustang)(?![<\w])/',"/(?<![>\w])honda(?![<\w])/");
    $replaceWith = array("<a href='fordmustangID'>ford mustang</a>","<a href='fordID'>ford</a>","<a href='hondaID'>honda</a>");
    echo preg_replace($remove, $replaceWith,$string);
    

    I used regular expressions with negative lookaheads and lookbehinds to make sure the portion of the string we’re replacing isn’t part of an alphanumeric sequence (like 12ford23 or afford) or touching the start or end tag of an element.

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

Sidebar

Related Questions

This is an example from: dofactory I reformatted it a bit for better fit
Does anyone know, or better yet have an example, of a WCF service that
Eclipse 3.5 has a very nice feature to generate Java hashCode() functions. It would
World's most convuluted title I know, an example should explain it better. I have
For example which is better: select * from t1, t2 where t1.country='US' and t2.country=t1.country
For example is it better to have: public class Case : EntityIdentifiable { public
Is it better to use $.get(http://www.example.com/mydirectory, function(data) { $(.someclass).html(data); }); or $('.tripPlannerBottom').load(http://www.example.com/mydirectory); any speed
Which one is better to use when it come to return value for example
I'm trying to learn Rails better by looking at example applications, and while looking
Below is a really nice time ago plugin for jQuery, very similar to what

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.