virtuexru Posted January 21, 2009 Share Posted January 21, 2009 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? Link to comment https://forums.phpfreaks.com/topic/141795-solved-adding-a-letter-to-a-string-should-be-simple-im-stumped/ Share on other sites More sharing options...
Mchl Posted January 21, 2009 Share Posted January 21, 2009 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); Link to comment https://forums.phpfreaks.com/topic/141795-solved-adding-a-letter-to-a-string-should-be-simple-im-stumped/#findComment-742356 Share on other sites More sharing options...
virtuexru Posted January 21, 2009 Author Share Posted January 21, 2009 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); ?> Link to comment https://forums.phpfreaks.com/topic/141795-solved-adding-a-letter-to-a-string-should-be-simple-im-stumped/#findComment-742363 Share on other sites More sharing options...
Mchl Posted January 21, 2009 Share Posted January 21, 2009 See array_walk_recursive to apply a function to all elements of an array Link to comment https://forums.phpfreaks.com/topic/141795-solved-adding-a-letter-to-a-string-should-be-simple-im-stumped/#findComment-742365 Share on other sites More sharing options...
nrg_alpha Posted January 21, 2009 Share Posted January 21, 2009 Alternatively: $string = str_replace('.JPG', 'X.JPG', $images[1][1]); Link to comment https://forums.phpfreaks.com/topic/141795-solved-adding-a-letter-to-a-string-should-be-simple-im-stumped/#findComment-742384 Share on other sites More sharing options...
nrg_alpha Posted January 21, 2009 Share Posted January 21, 2009 $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... Link to comment https://forums.phpfreaks.com/topic/141795-solved-adding-a-letter-to-a-string-should-be-simple-im-stumped/#findComment-742388 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.