搜索文本中的单词,并将其替换

问题描述 投票:0回答:1

我很难找到解决问题的方法。

我在变量中有一个像这样的数组:

$myarray = Array (
[0] => Orange
[1] => Black
[2] => White
[3] => Yellow
[4] => Red
);

[基本上,我需要在字符串中搜索数组的单词,并用相同的单词替换它们,但是使用链接。

例如,来自:

$string = "My content contains orange and also black";

收件人:

$string = "My content contains <a href="www.domain.com/orange">orange</a> and also blue";
php
1个回答
0
投票

您可以使用str_replace,检查link

$string = "My content contains orange and also black";
$string = str_replace("orange","<a href='http://www.example.com/orange'>orange</a>",$string);
© www.soinside.com 2019 - 2024. All rights reserved.