6

Using PHP, how can i replace a word in a string with different links.

I want to replace the word web development with different links in order

This web development company is great. A good web development starts with...

like so

This web development company is great. A good web development starts with...

i have tried using str_ireplace but then both links lead to the same site.

Here is my code

<?php

$text = 'This web development company is great. A good web development starts with...';


$replaced = str_ireplace(array('web development', 'web development'),array('<a href="http://google.com">web development</a>', '<a href="http://yahoo.com">web development</a>'), $text);

echo $replaced;
Ronny K
  • 3,641
  • 4
  • 33
  • 43
  • How should php be able to know which link to replace "web development" with? By order? – Tom G Aug 09 '13 at 15:43
  • Are the links you're wanting to use for replacement always going to appear in the same order? Do you have any control over the original content, ie changing web development to a more usable placeholder? –  Aug 09 '13 at 15:45
  • The first replacement link in the array should replace the first word. – Ronny K Aug 09 '13 at 15:47
  • 1
    possible duplicate of [PHP: str\_replace that only acts on the first match?](http://stackoverflow.com/questions/1252693/php-str-replace-that-only-acts-on-the-first-match) – vascowhite Aug 09 '13 at 15:52

4 Answers4

5

I think you can use the preg_replace() function, as the following:

$pattern = '/(web development)(.*)(web development)/';
$replace = '<a href="http://google.com">web development</a>$2<a href="http://yahoo.com">web development</a>';
$result = preg_replace($pattern, $replace, $text);
echo $result;

Hope helps!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Hanfeng
  • 645
  • 5
  • 11
1

Try with regex like:

<?php
$text = 'This web development company is great. A good web development starts with...';

$regex = "/[^>]web development[^<]/i";
$replace = array(' <a href="http://google.com">web development</a> ', 
' <a href="http://yahoo.com">web development</a> ');
$count = 1;
$replaced = preg_replace(array($regex,$regex), $replace, $text, $count);

echo $replaced;
0

Like in this topic you can do something like that (with preg_replace) :

$str = 'abc and abc'; 
$str = preg_replace('/abc/', 'google', $str, 1); 
$str = preg_replace('/abc/', 'yahoo', $str, 1);
echo $str;

It will display : "google and yahoo"

(I hope I understood your need)

Community
  • 1
  • 1
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
0

I think you can do it with preg_replace.

Searches subject for matches to pattern and replaces them with replacement.

You can set a limit to the times the word is going to be replaced.

limit: The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

So, I think you can do a function that first, count the ocurrences of "Web Development" has your text, and then you execute:

$text = preg_replace("web development", "<a href='http://www.exemple.com'>web development</a>", $text, 1);

x times.

Remember the function preg_replace:

preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

See reference in the PHP Manual.

Lan
  • 709
  • 1
  • 8
  • 16