Jump to content


Photo

replace last ocurrence


  • Please log in to reply
7 replies to this topic

#1 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 02 October 2006 - 01:42 PM

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 think

PS please sympatize! I did the Great North Run yesterday!!!!
follow me on twitter @PHPsycho

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 02 October 2006 - 03:42 PM

The .+ is greedy and gobbles everything. The engine then gives everything back until it finds a / (which would be the last one).
	$string = '/path/to/image/dir/';
	echo preg_replace('%(.+)/%', '\1/thumb/', $string);

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 02 October 2006 - 04:34 PM

The .+ is greedy and gobbles everything. The engine then gives everything back until it finds a / (which would be the last one).

	$string = '/path/to/image/dir/';
	echo preg_replace('%(.+)/%', '\1/thumb/', $string);


with that being said, do you think it would be safer to use .* in case you're matching a root level directory?
<?php
$string = '/';
echo preg_replace('|(.*)/|', '\1/thumb/', $string);
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 02 October 2006 - 05:25 PM

Good catch.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 02 October 2006 - 05:28 PM

Good catch.


thanks... not often i have one of those with regex ;)
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 Nicklas

Nicklas
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts
  • Locationwww.nswardh.com

Posted 02 October 2006 - 06:49 PM

you want to replace the last / of your path to /thumb/ ?

why dont you just thumb/ to the end of the path?
$string = '/path/to/image/dir/';
echo $string . 'thumb/'; // ouput: /path/to/image/dir/thumb/


#7 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 02 October 2006 - 07:00 PM

you want to replace the last / of your path to /thumb/ ?

why dont you just thumb/ to the end of the path?

$string = '/path/to/image/dir/';
echo $string . 'thumb/'; // ouput: /path/to/image/dir/thumb/


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 '/'
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#8 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 03 October 2006 - 09:52 AM

Thank you. My brain is still not back on track!! ;)
follow me on twitter @PHPsycho




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users