0

I am having an issue with the following script that adds links to a block of text, by replacing them with matches.

It works perfectly for what it was designed for, creating links with vehicle ID's in, but I would like to change it for SEO purposes to the vehicle names the issue is that the script re-reads the replaced text and overwrites it.

  • For example: $string = "blah blah ford mustang blah blah"
  • It should become: "blah blah ford mustang"
  • Instead it becomes: "Ford&model=Mustang">ford mustang"

Is it possible to make it ignore the text within hyperlinks?


// The following script was written by Walkerneo

function replaceLinks($replacements, $string){
    foreach($replacements as $key=>$val){
        $key=strtolower((string)$key);
        $newReplacements[$key]=array();
        $newReplacements[$key]['id']=$val;
        $newReplacements[$key]['b']=array();
        $newReplacements[$key]['a']=array();
        foreach($replacements as $key2=>$val2){
            $key2=(string)$key2;
            $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)));

            }
            if(strlen($key2)>strlen($key) && ($pos=strpos($key2,$key))!==false){
                if($pos!=0){
                    $newReplacements[$key]['b'][]=substr($key2,0,$pos);
                }
                if(($end=$pos+strlen($key))!=strlen($key2)){
                    $newReplacements[$key]['a'][]=substr($key2,$end);
                }
            }
        }
    }
    foreach($newReplacements as $key=>$item){
        $tmp="/(?<![\w>])";
        foreach($item['b'] as $b){
            $tmp.="(?<!$b)";
        }
        $tmp.="($key)";
        foreach($item['a'] as $a){
            $tmp.="(?!$a)";
        }
        $tmp.='/ie';
        $replacementMatches[]=$tmp;
    }
    return preg_replace($replacementMatches,'"<a href=\"".$newReplacements[strtolower("$1")]["id"]."\">$1</a>"' ,$string);

}

//$replaceWith = array('ford mustang'=>123,'ford'=>42,'honda'=>324);  <-- this is what the script was written for
$replaceWith = array('ford mustang'=> 'make=Ford&model=Mustang','ford'=> 'make=Ford','honda'=> 'make=Honda');
$string = "That is a very nice ford mustang, if only every other ford was quite as nice as this honda";

echo replaceLinks($replaceWith,$string);
Chris
  • 5,516
  • 1
  • 26
  • 30
  • What have you tried? It's generally frowned upon to drop a script on Stackoverflow and ask for it to be altered to suit your purposes. Why don't you ask Walkerneo? – Mike B Mar 13 '12 at 20:05
  • I tried fiddling around with the contents of 'foreach($newReplacements as $key=>$item){' but genuinely am not sure how it works, so most of the time it just errors. I didn't know it was possible to message members - I will now :) Sorry! – Chris Mar 13 '12 at 20:14
  • You can't message members on stackoverflow. I was referring to the comment at the top of your code: `//The following script was written by Walkerneo` – Mike B Mar 13 '12 at 20:37
  • Ah right haha - he got in touch with me a minute ago - this has been solved by Walkerneo @stackoverflow :) Cheers – Chris Mar 13 '12 at 20:42

0 Answers0