Hertus Hertus - 10 months ago 37
PHP Question

Replace word of an arrays output

I have this array:

$array = array('name'=>'test','server'=>'zangarmarsh','fields'=>'items,stats');
$type = 'character';
$r = $client->fetch($type,$array);
echo '<pre>';
echo '</pre>';

The output:

[result] => Array

[thumbnail] => hellscream/74/113337162-avatar.jpg

Now I echo out the picture:

echo "<img src='http://render-api-us.worldofwarcraft.com/static-render/us/" . $r['result']['thumbnail'] . "' alt='error'>";

Now i want to change the word "avatar" (from the arrays´ output) with the word "profilemain". I know that you can do it with echo str_replace, but i can´t get it work.


You could use strpos($yourString, $substring) to get the beginning index of 'avatar', then use the length of 'avatar' to get the end index. Cut away the first and the last parts of the string into two separate string variables, then combine them around the string 'profilemain'.

$thumb = 'hellscream/74/113337162-avatar.jpg';

// This gets you everything up to 'avatar' in $thumb
$thumbPre = substr($thumb, 0, strpos($thumb, 'avatar'));
// This gets you everything after 'avatar' in $thumb   
$thumbPost = substr($thumb, strpos($thumb, 'avatar') + strlen('avatar'));

// Sandwich 'profilemain' in between the two
$newString = $thumbPre.'profilemain'.$thumbPost;

Hopefully that helps!