ToonMariner Posted October 2, 2006 Share Posted October 2, 2006 OK I got writers block...I know this is easy.have a string of a full path to a file want to replace the last '/' with '/thumb/'sure i can user just str_replace for this but i just can't thinkPS please sympatize! I did the Great North Run yesterday!!!! Quote Link to comment Share on other sites More sharing options...
effigy Posted October 2, 2006 Share Posted October 2, 2006 The[tt] .+ [/tt] is greedy and gobbles everything. The engine then gives everything back until it finds a / (which would be the last one).[code] $string = '/path/to/image/dir/'; echo preg_replace('%(.+)/%', '\1/thumb/', $string);[/code] Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 2, 2006 Share Posted October 2, 2006 [quote author=effigy link=topic=110239.msg445316#msg445316 date=1159803770]The[tt] .+ [/tt] is greedy and gobbles everything. The engine then gives everything back until it finds a / (which would be the last one).[code] $string = '/path/to/image/dir/'; echo preg_replace('%(.+)/%', '\1/thumb/', $string);[/code][/quote]with that being said, do you think it would be safer to use [tt].*[/tt] in case you're matching a root level directory?[code]<?php$string = '/';echo preg_replace('|(.*)/|', '\1/thumb/', $string);?>[/code] Quote Link to comment Share on other sites More sharing options...
effigy Posted October 2, 2006 Share Posted October 2, 2006 Good catch. Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 2, 2006 Share Posted October 2, 2006 [quote author=effigy link=topic=110239.msg445409#msg445409 date=1159809933]Good catch.[/quote]thanks... not often i have one of those with regex ;) Quote Link to comment Share on other sites More sharing options...
Nicklas Posted October 2, 2006 Share Posted October 2, 2006 you want to replace the last / of your path to /thumb/ ?why dont you just [b]thumb/[/b] to the end of the path?[code]$string = '/path/to/image/dir/';echo $string . 'thumb/'; // ouput: /path/to/image/dir/thumb/[/code] Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 2, 2006 Share Posted October 2, 2006 [quote author=Nicklas link=topic=110239.msg445494#msg445494 date=1159814971]you want to replace the last / of your path to /thumb/ ?why dont you just [b]thumb/[/b] to the end of the path?[code]$string = '/path/to/image/dir/';echo $string . 'thumb/'; // ouput: /path/to/image/dir/thumb/[/code][/quote]because your path may be referring to a specific file. for instance, if you grab the current URL and based on the location of the current file, you want to get the thumbnail directory, you'd want to start with the last slash, NOT with the end of the string. there are several instances where you'd want to use a check such as the regex that effigy provided as opposed to making assumptions that the path is always going to end with '/' Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted October 3, 2006 Author Share Posted October 3, 2006 Thank you. My brain is still not back on track!! ;) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.