Jump to content

[SOLVED] Adding a letter to a string.. should be simple, I'm stumped.


virtuexru

Recommended Posts

PHP Gurus!

 

How would I be able to take one of my arrays, which for example is:

 

$images[1][1];

 

which contains: h**p://website.com/website/data/pix/20081119/19667358_1.JPG

 

Basically, what I need to do is add an "X" right after BEFORE the ".JPG" so the result needs to be:

 

h**p://website.com/website/data/pix/20081119/19667358_1X.JPG

 

Anyone got any ideas?

If it is always adding X before last 4 characters, you can use substr

 

$string = substr($images[1][1],0,-4)."X".substr($images[1][1],-4);

 

Awesome. Thank you Mchl!

 

That worked, I was wondering if there was anyway to do this with ereg so I don't have to run this 8 times (for 8 images)?

 

Would something like this work or am I retarded:

 

<?php
$reg_ex       = '.JPG';
$replace_word = "X.JPG"; 

ereg_replace($reg_ex, $replace_word, $images);
?>

$str = array('h**p://website.com/website/data/pix/20081119/19667358_1.JPG', 'h**p://website.com/website/data/pix/472835_1.JPG', 'h**p://website.com/website/data/pix/AA_463_1.JPG');
$str = str_replace('.JPG','X.JPG', $str);
echo '<pre>'.print_r($str, true);

 

Output:

Array
(
    [0] => h**p://website.com/website/data/pix/20081119/19667358_1X.JPG
    [1] => h**p://website.com/website/data/pix/472835_1X.JPG
    [2] => h**p://website.com/website/data/pix/AA_463_1X.JPG
)

 

For simple replacements, simply plugging the array into str_replace is in all likeyhood quicker than array_walking...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.