1

My code is

$words = array();
$links = array();
$result = mysql_query("SELECT `keyword`, `link` FROM `articles` where `link`!='".$act."' ") 
or die(mysql_error());
$i = 0;
while($row = mysql_fetch_array( $result )) 
{
if (!empty($row['keyword']))
{
$words[$i] = '/(?<!(src="|alt="))'.$row['keyword'].'/i';
$links[$i] = '<a href="'.$row['link'].'.html" class="ared">'.$row['keyword'].'</a>';
$i++;
}
}
$text = preg_replace($words, $links, $text);

I want to replace Hello with Guys except img src and alt.

From

Say Hello my dear <img src="say-hello-my-dear.jpg" alt="say hello my dear" />

I want

Say Guys my dear <img src="say-hello-my-dear.jpg" alt="say hello my dear" />

The current code, replaces only when my keyword has only 1 word.

3 Answers3

1

EDIT: the previsouly suggested correction was not relevant.

Still:

simon.denel
  • 780
  • 6
  • 23
  • @user2233781: I do not understand what '?' stands for. Personaly, I would have written: $words[$i] = '/!(src="|alt="|src=\'|alt=\')'.$row['keyword'].'/i'; – simon.denel Apr 01 '13 at 23:08
  • I think I did not understand the problem. As you can see, this simple example is perfectly working: http://ideone.com/MDR5HF – simon.denel Apr 01 '13 at 23:29
  • hello - you can see it here - http://www.tedsdiets.com/beyonce-lemonade-diet.html - it replaces the word "lemonade" but it replaces also the .jpg lemonadelemonade-diet.jpg" – user2233781 Apr 01 '13 at 23:38
  • and in your example, the keywords should be hello and goodbye – user2233781 Apr 01 '13 at 23:47
1

EDIT: I can't believe it took me that long to understand that you're trying to parse big chunks of HTML with regular expressions. Read the answer to this question: RegEx match open tags except XHTML self-contained tags

Community
  • 1
  • 1
pevinkinel
  • 391
  • 3
  • 17
0

Edit: I updated the code to work better.

I'm unsure exactly what the issue is but looking at your code I wouldn't be surprised that the negative look behind regex isn't matching multiple word strings where the "keyword" is not the first word after the src or alt. It might possible to beef up the regex, but IMHO a complicated regex might be a little too brittle for your html parsing needs. I'd recommend doing some basic html parsing yourself and doing a simple string replace in the right places.

Here's some basic code. There is certainly a much better solution than this, but I'm not going to spend too much time on this. Probably, rather than inserting html in a text node, you should create a new html a element with the right attributes. Then you wouldn't have to decode it. But this would be my basic approach.

$text = "Lorem ipsum <img src=\"lorem ipsum\" alt=\"dolor sit amet\" /> dolor sit amet";

$result = array(
  array('keyword' => 'lorem', 'link' => 'http://www.google.com'),
  array('keyword' => 'ipsum', 'link' => 'http://www.bing.com'),
  array('keyword' => 'dolor sit', 'link' => 'http://www.yahoo.com'),
);

$doc = new DOMDocument();
$doc->loadHTML($text);
$xpath = new DOMXPath($doc);

foreach($result as $row) {
  if (!empty($row['keyword'])) {
    $search = $row['keyword'];
    $replace = '<a href="'.$row['link'].'.html" class="ared">'.$row['keyword'].'</a>';

    $text_nodes = $xpath->evaluate('//text()');
    foreach($text_nodes as $text_node) {
      $text_node->nodeValue = str_ireplace($search, $replace, $text_node->nodeValue);
    }
  }
}

echo html_entity_decode($doc->saveHTML());

The $result data structure is meant to be similar to result of your mysql_fetch_array(). I'm only getting the children of the root for the created html DOMDocument. If the $text is more complicated, it should be pretty easy to traverse more thoroughly through the document. I hope this helps you.

  • this is my text ` significant lemonade amount of weight. Beyonce looks spectacular just because of the diet she consumed. If you want to have a perfect figure like Beyonces then you have got to check out Beyonce lemonade Diet. ` and this is the result `significant amount of weight. Beyonce looks spectacular just because of the diet she consumed. If you want to have a perfect figure like Beyonces then you have got to check out Beyonce lemonade Diet. ` The image is missing! – user2233781 Apr 02 '13 at 00:22