我需要替换这个HTML代码:
Actors: Mamoru Miyano, Kappei Yamaguchi, Aya Hirano
使用此代码:
Actors: <a href="http://example.com/actor/Mamoru Miyano">Mamoru Miyano</a>, <a href="http://example.com/actor/Kappei Yamaguchi">Kappei Yamaguchi</a>, <a href="http://example.com/actor/Aya Hirano">Aya Hirano</a>
有可能吗?
在控制面板中我看不到演员列表我只看到这个:[xfvalue_actors]
我试图替换为:
<a href="http://myfilms.ga/actor/<?php $str = '[xfvalue_actors]'; echo preg_replace('/, /', '"></a><a href="http://myfilms.ga/actor/', $str, 20); ?>/">[xfvalue_actors]</a><br />
但我明白了:
<a href="http://myfilms.ga/actor/Mamoru Miyano"></a><a href="http://myfilms.ga/actor/Kappei Yamaguchi"></a><a href="http://myfilms.ga/actor/Aya Hirano">Mamoru Miyano, Kappei Yamaguchi, Aya Hirano</a><br />
答案 0 :(得分:0)
如果要替换字符串的一部分
,可以使用str_replace
$old = "Actors: Mamoru Miyano, Kappei Yamaguchi, Aya Hirano";
$new = "Actors: <a href="http://example.com/actor/Mamoru Miyano>Mamoru Miyano</a>, <a href="http://example.com/actor/Kappei Yamaguchi>Kappei Yamaguchi</a>, <a href="http://example.com/actor/Aya Hirano>Aya Hirano</a>";
str_replace($old,$new,$your_string)
但在你的情况下,你指的是HTML。所以你可以使用jquery
var replaced = $("body").html().replace('Actors: Mamoru Miyano, Kappei Yamaguchi, Aya Hirano','The new string');
$("body").html(replaced);
答案 1 :(得分:0)
如果您尝试根据某种条件进行更改..如果循环(php)
<?php if(condition){ ?> Actors: <a href="http://example.com/actor/Mamoru Miyano>Mamoru Miyano</a>, <a href="http://example.com/actor/Kappei Yamaguchi>Kappei Yamaguchi</a>, <a href="http://example.com/actor/Aya Hirano>Aya Hirano</a><?php }else { ?>Actors: Mamoru Miyano, Kappei Yamaguchi, Aya Hirano <?php } ?>
答案 2 :(得分:0)
你可以尝试一下并告诉我它是如何工作的吗?
<?php
$str = '[xfvalue_actors]';
$arrActors = explode(',', $str);
$out = '';
foreach ($arrActors as $actor) {
$out .= "<a href='http://myfilms.ga/actor/{$actor}'>{$actor}</a><br />,";
}
echo "Actors: " . substr($out, 0, -1);
?>
结果我得到了:
Actors: <a href='http://myfilms.ga/actor/Mamoru Miyano'>Mamoru Miyano</a><br />,<a href='http://myfilms.ga/actor/ Kappei Yamaguchi'> Kappei Yamaguchi</a><br />,<a href='http://myfilms.ga/actor/ Aya Hirano'> Aya Hirano</a><br />
希望这就是你所需要的! :)