Using PHP, how can i replace a word in a string with different links.
使用PHP,如何用不同的链接替换字符串中的单词。
I want to replace the word web development with different links in order
我想用不同的链接取代word web开发
This web development company is great. A good web development starts with...
这个web开发公司很棒。一个好的web开发始于……
like so
像这样
This web development company is great. A good web development starts with...
这个web开发公司很棒。一个好的web开发始于……
i have tried using str_ireplace
but then both links lead to the same site.
我试过使用str_ireplace,但是两个链接都指向同一个站点。
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;
4 个解决方案
#1
5
I think you can use the preg_replace()
function, as the following:
我认为可以使用preg_replace()函数,如下所示:
$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!
希望会有帮助!
#2
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;
#3
0
Like in this topic you can do something like that (with preg_replace) :
就像在这个主题中,你可以做类似的事情(使用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"
将显示:“谷歌和yahoo”
(I hope I understood your need)
(我希望我理解你的需要)
#4
0
I think you can do it with preg_replace
.
我想你可以用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).
限制:每个主题字符串中每个模式的最大可能替换。默认为-1(没有限制)。
So, I think you can do a function that first, count the ocurrences of "Web Development" has your text, and then you execute:
所以,我认为你可以做一个函数,首先,数一下“Web开发”有你的文本,然后你执行:
$text = preg_replace("web development", "<a href='http://www.exemple.com'>web development</a>", $text, 1);
x times.
x乘以。
Remember the function preg_replace:
记住这个函数preg_replace:
preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
See reference in the PHP Manual.
参见PHP手册中的参考。
#1
5
I think you can use the preg_replace()
function, as the following:
我认为可以使用preg_replace()函数,如下所示:
$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!
希望会有帮助!
#2
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;
#3
0
Like in this topic you can do something like that (with preg_replace) :
就像在这个主题中,你可以做类似的事情(使用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"
将显示:“谷歌和yahoo”
(I hope I understood your need)
(我希望我理解你的需要)
#4
0
I think you can do it with preg_replace
.
我想你可以用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).
限制:每个主题字符串中每个模式的最大可能替换。默认为-1(没有限制)。
So, I think you can do a function that first, count the ocurrences of "Web Development" has your text, and then you execute:
所以,我认为你可以做一个函数,首先,数一下“Web开发”有你的文本,然后你执行:
$text = preg_replace("web development", "<a href='http://www.exemple.com'>web development</a>", $text, 1);
x times.
x乘以。
Remember the function preg_replace:
记住这个函数preg_replace:
preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
See reference in the PHP Manual.
参见PHP手册中的参考。