Re: Replace in a string with regex

From: Date: Wed, 22 Jul 2009 13:50:17 +0000
Subject: Re: Replace in a string with regex
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-295679@lists.php.net to get a copy of this message
You can disregard this, it's wrong (I missed a part of the requirements :) and there's other solutions already provided (my email client is weird when you switch to a folder it always displays the first entry as the last read, so sometimes I miss that there are new posts above... I just switched a few weeks ago. Cheers, Rob. Robert Cummings wrote:
rszeus wrote:
Hello, I’m tryng to make some replacements on a string. Everything goês fine until the regular expression. $file = "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg"; echo $a = str_replace(array(7a45gfdi6icpan1jtb1j99o925, 'temp/',’_([0-9])’), array(“test”,"",””), $file) The idea is to remove /temp and the last _1 from the file name..but i’m only getting this: screens/test_1_main.jpg I want it to be: screens/test_main.jpg
Sometimes it's helpful to break a problem into smaller problems: <?php
     $a = str_replace( '/temp/', '/', $file );
     $a = preg_replace( '#_\d+_#', '_', $a );
     echo $a."\n";
?> Cheers, Rob.
-- http://www.interjinn.com Application and Templating Framework for PHP

« previous php.general (#295679) next »