具有如下数组中的关键字列表:
$keywordlist = array(
'Apple iPhone' => 'http://www.example.com/apple/iphone/',
'Apple iPad' => 'http://www.example.com/apple/ipad',
'Samsung Galaxy Ace' => 'http://www.example.com/samsung/galaxy-ace',
'Samsung Galaxy Nexus' => 'http://www.example.com/samsung/galaxy-nexus',
'Samsung' => 'http://www.example.com/samsung/',
'Apple' => 'http://www.example.com/apple/'
);我想用与它们相关的URL替换关键字。
我试着循环使用str_replace和preg_replace,但这取代了所有关键词中的制造商名称,所以所有关键字都变成了“三星”和“苹果”的链接。我有点搞不懂下一步该往哪里去,谁有什么建议吗?
编辑:
我用下面的代码循环通过-
foreach($keywordlist as $name => $link){
$content = str_replace($name, '<a href="'.$link.'">'.$name.'</a>', $content);
}解决方案:
我认为问题在于我要替换的链接文本。然后,它又被其他词组替换为类似的关键字,下面是我要做的。
有人想出更好的办法吗?
$content = "<p>This Apple iPhone is from Apple</p>
<p>This Apple iPad is from Apple</p>
<p>This Samsung Galaxy Ace is from Samsung</p>
<p>This Samsung Galaxy Nexus is from Samsung</p>
<p>This is a Samsung</p>
<p>This is an Apple</p>";
$keywordlist = array(
'Apple iPhone' => '[1]',
'Apple iPad' => '[2]',
'Samsung Galaxy Ace' => '[3]',
'Samsung Galaxy Nexus' => '[4]',
'Samsung' => '[5]',
'Apple' => '[6]'
);
$content = str_replace(array_keys($keywordlist), array_values($keywordlist), $content);
$urllist = array(
'[1]' => '<a href="http://www.example.com/apple/iphone/">Apple iPhone</a>',
'[2]' => '<a href="http://www.example.com/apple/ipad">Apple iPad</a>',
'[3]' => '<a href="http://www.example.com/samsung/galaxy-ace">Samsung Galaxy Ace</a>',
'[4]' => '<a href="http://www.example.com/samsung/galaxy-nexus">Samsung Galaxy Nexus</a>',
'[5]' => '<a href="http://www.example.com/samsung/">Samsung</a>',
'[6]' => '<a href="http://www.example.com/apple/">Apple</a>'
);
$content = str_replace(array_keys($urllist), array_values($urllist), $content);
echo $content;输出:
这个苹果iPhone来自苹果
这个苹果iPad来自苹果
这个三星Galaxy Ace来自三星
这个三星Galaxy Nexus来自三星
这是一个三星
这是一个苹果
发布于 2012-07-09 09:20:42
对于您正在尝试的内容,str_replace()应该足够了。
foreach($keywordlist as $key => $link) {
$content = str_replace($key, '<a href="$link">$key</a>', $content);
}正如我所评论的,这将不能像预期的那样工作,因为键上重复的关键字。
你需要一个固定的格式来表示一个键。我建议的是类似[Apple]和[Apple iPad]的东西。当您实现类似于此的内容时,每个关键字都会有所不同,尽管它们内部包含相同的内部代码。
更新后的关键字结构将在wards之后出现。
$keywordlist = array(
'[Apple iPhone]' => 'http://www.example.com/apple/iphone/',
'[Apple iPad]' => 'http://www.example.com/apple/ipad',
'[Samsung Galaxy Ace]' => 'http://www.example.com/samsung/galaxy-ace',
'[Samsung Galaxy Nexus]' => 'http://www.example.com/samsung/galaxy-nexus',
'[Samsung]' => 'http://www.example.com/samsung/',
'[Apple]' => 'http://www.example.com/apple/'
);如果这样,使用选项是不可行的,因为这大大增加了开发内容的复杂性,因为需要用[]包装每一个关键字文本。
发布于 2012-07-09 09:22:28
对我来说,这听起来有点没用,因为你有和值一样的键。也许我误解了你的意图。但是这里是这样的:这使得键与值(Url)相同。
$newkeywordlist = array_combine (array_values($keywordlist),array_values($keywordlist));http://nl.php.net/manual/en/function.array-combine.php
http://nl.php.net/manual/en/function.array-values.php
发布于 2012-07-09 09:32:08
你可以用这个:
$s = "this is a test";
$arr = array(
"this" => "THIS",
"a" => "A"
);
echo str_replace(array_keys($arr), array_values($arr),$s);产出:
THIS is A testhttps://stackoverflow.com/questions/11392152
复制相似问题