Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.