Nevermind23
Nevermind23

Reputation: 395

Replace word with link in php

I need to replace this html code:

Actors: Mamoru Miyano, Kappei Yamaguchi, Aya Hirano

with this code:

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>

Is it possible?

In control panel i cannot see actors list i see only this: [xfvalue_actors]

and i tried to replace as this:

<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 />

but i get it:

<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 />

Upvotes: 1

Views: 562

Answers (3)

Antoan Milkov
Antoan Milkov

Reputation: 2228

Can you try this and tell me how it is working?

<?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);
?>

As a result I am getting:

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 />

Hope this is what you need! :)

Upvotes: 2

subas chandran
subas chandran

Reputation: 13

if you are trying to change based on a certain condition..try if loop(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 } ?>

Upvotes: 0

Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17566

You can use str_replace if you want to replace a part of string

$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)

But in your case you are referring to HTML . so may be you can use jquery

var replaced = $("body").html().replace('Actors: Mamoru Miyano, Kappei Yamaguchi, Aya Hirano','The new string');
$("body").html(replaced);

Upvotes: 0

Related Questions