RE: Replace in a string with regex

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi. It Works to remove the _1 but it doesn't replace '7a45gfdi6icpan1jtb1j99o925' for 'test'

Thank you

-----Mensagem original-----
De: Eddie Drapkin [mailto:oorza2k5@xxxxxxxxx] 
Enviada: quarta-feira, 22 de Julho de 2009 13:11
Para: rszeus
Cc: php-general@xxxxxxxxxxxxx
Assunto: Re:  Replace in a string with regex

On Wed, Jul 22, 2009 at 8:02 AM, rszeus<rszeus@xxxxxxxxx> 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
>
>
>
> Thank you
>
>
>
>

If you're trying to do a regular expression based search and replace,
you probably ought to use preg_replace instead of str_replace, as
str_replace doesn't parse regular expressions.

Try this one out, I think I got what you wanted to do:

<?php

$file = "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";

echo preg_replace('#(screens/)temp/(.+?)_1(_main\.jpg)#', '$1$2$3', $file);


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux