0

I'm trying to do something simple but i'm failing : I want to use preg_replace in PHP to make keywords become a link, but i don't want to insert it in headings.

My actual preg_replace :

$data_replace = '<a href="'.$link.'">$1</a>';
$word_to_replace = "keyword";
$data_source = "<h1>keyword</h1><p>keyword</p>"
$pattern = "/\\b(?!<h[0-6]*\>)(.*?)(" . trim($word_to_replace) . ")(.*?)(?!<\/h[0-6]>)\\b/";
preg_replace($pattern, $data_replace, $data_source, 1);

And I want to get :

<h1>keyword</h1><p><a href="http://...">keyword</a></p>

For now, my output gives :

<h1><a href="http://...">keyword</a></h1><p>keyword</p>

Where am i wrong in my regex pattern ? Thanks !

Ali
  • 56,466
  • 29
  • 168
  • 265
Matt Loye
  • 1,301
  • 2
  • 11
  • 14
  • You [shouldn't use regex to parse HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454), use a [DOM parser](http://www.php.net/manual/en/class.domdocument.php) instead. – Sam Apr 09 '14 at 21:27
  • I know but i want to know the answer for my knowledge :) – Matt Loye Apr 09 '14 at 22:09

1 Answers1

1

Try this:

<?php

$data_replace = '<a href="'.$link.'">$1</a>';
$words = ["keyword","foo"];
$out = "<h1>keyword</h1><p>keyword</p>";
foreach ($words as $word){
    $pattern = "/(<[^h].*?>)($word)(<\/)/";
    $out = preg_replace($pattern, "\\1<a href=\"#\">$word</a>\\3", $out);
}
var_dump($out);

Produces:

dejavu:~ jancha$ php -q aa.php
string(46) "<h1>keyword</h1><p><a href="#">keyword</a></p>"

p.s. this has enhancement for multiple keywords.

jancha
  • 4,916
  • 1
  • 24
  • 39