RE: Replace in a string with regex

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

 



No, sory, my bad typing. It's not the problem..
I have the same on both caxses, only chnage the variable $id 
 $file = "screen/temp/7a45gfdi6icpan1jtb1j99o925_1_mini.jpg" 
 $id = '70';
 echo preg_replace('#(screen/)temp/(.+?)_1(.+?\.jpg)#', '$1'.$id,  $file);
 Get: 0
 
file = "screen/temp/7a45gfdi6icpan1jtb1j99o925_1_mini.jpg";
$id = test;
echo preg_replace('#(screen/)temp/(.+?)_1(.+?\.jpg)#', '$1'.$id,  $file);

Get: screen/test

What is wrong with having na integer on the var ?

Thank you

-----Mensagem original-----
De: Jim Lucas [mailto:lists@xxxxxxxxx] 
Enviada: quarta-feira, 22 de Julho de 2009 18:44
Para: rszeus
Cc: 'Kyle Smith'; 'Eddie Drapkin'; ash@xxxxxxxxxxxxxxxxxxxx; php-general@xxxxxxxxxxxxx
Assunto: Re:  Replace in a string with regex

rszeus wrote:
> Thank you. I undestand now.
> 
> Anh it’s already workyng the replacemente with letteres. Bu if the variabele is a number it doens’t work. Any ideas ?
> 
>  
> 
> $file = "screen/temp/7a45gfdi6icpan1jtb1j99o925_1_mini.jpg";
> 
>  
> 
> $id = '70';
> 
>  echo preg_replace('#(Iscreen/)temp/(.+?)_1(.+?\.jpg)#', '$1'.$id,  $file);
> 

You have a great big capital I in there...  Try removing that and see
what happens.


> I get 0
> 
>  
> 
>  
> 
> $id = 'test';
> 
>  echo preg_replace('#(screen/)temp/(.+?)_1(.+?\.jpg)#', '$1'.$id,  $file);
> 
> I get screen/test
> 
>  
> 
> Any ideas ?
> 
>  
> 
> Thank you
> 
>  
> 
> De: Kyle Smith [mailto:kyle.smith@xxxxxxxxxxxxxx] 
> Enviada: quarta-feira, 22 de Julho de 2009 17:22
> Para: rszeus
> Cc: 'Eddie Drapkin'; ash@xxxxxxxxxxxxxxxxxxxx; 'Jim Lucas'; php-general@xxxxxxxxxxxxx
> Assunto: Re:  Replace in a string with regex
> 
>  
> 
> The first match inside () is assigned to $1, the second is assigned to $2, and so on.
> 
> rszeus wrote: 
> 
> Hi, 
> It doens't work.
> I get 0_main.jpg if I do that..
> I don't undestand the point of $1 $2 and $3..
> In preg_replace('#(screens/)temp/(.+?)_1(_main\.jpg)#', what will be $1 and $2 and $3 ?
> $ " I already knwo it's the (.+?) but the others didnt' get it.
>  
> Thank you...
>  
> -----Mensagem original-----
> De: Eddie Drapkin [mailto:oorza2k5@xxxxxxxxx] 
> Enviada: quarta-feira, 22 de Julho de 2009 16:03
> Para: ash@xxxxxxxxxxxxxxxxxxxx
> Cc: Jim Lucas; rszeus; php-general@xxxxxxxxxxxxx
> Assunto: Re:  Replace in a string with regex
>  
> On Wed, Jul 22, 2009 at 11:01 AM, Ashley
> Sheridan <mailto:ash@xxxxxxxxxxxxxxxxxxxx> <ash@xxxxxxxxxxxxxxxxxxxx> wrote:
>   
> 
> On Wed, 2009-07-22 at 07:54 -0700, Jim Lucas wrote:
>     
> 
> rszeus wrote:
>       
> 
> Thank you.
>  
> What about instead test i want to insert a variable ?
> Like
> $id = "30";
> $file = "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
> echo preg_replace('#(screens/)temp/(.+?)_1(_main\.jpg)#', '$1$id$3', $file);
>         
> 
> Sure that can be done.  But you will need to change the second argument
> to have double quotes so it will be parsed by PHP.
>  
> Then I would surround YOUR variable with curly brackets to separate it
> from the rest.
>  
> echo preg_replace('#(screens/)temp/.+?_1(_main\.jpg)#',
>                   "$1{$id}$3",
>                   $file);
>  
>       
> 
> I am confusing " and '.
>  
> Thank you
>  
>  
> -----Mensagem original-----
> De: Eddie Drapkin [mailto:oorza2k5@xxxxxxxxx]
> Enviada: quarta-feira, 22 de Julho de 2009 14:12
> Para: rszeus
> Cc: php-general@xxxxxxxxxxxxx
> Assunto: Re:  Replace in a string with regex
>  
> On Wed, Jul 22, 2009 at 9:07 AM, rszeus <mailto:rszeus@xxxxxxxxx> <rszeus@xxxxxxxxx> wrote:
>         
> 
> 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 <mailto:rszeus@xxxxxxxxx> <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);
>  
>  
>           
> 
> In the second parameter, $2 is the string you'd want to replace to
> test so change '$1$2$3' to '$1test$3'.
>  
> It seems like you're having trouble with regular expressions, may I
> suggest you read up on them?
>  
> http://www.regular-expressions.info/ is a pretty great free resource,
> as ridiculous as the design is.
>  
>  
>         
> 
>  
>  
>       
> 
> I tested this, with the double quotes and the curly braces around the
> middle argument (to avoid PHP getting confused) and it didn't recognise
> the matches by the numbered $1, $3, etc. I know I'm not the op who asked
> the original question, but it might help him/her?
>  
> Thanks
> Ash
> www.ashleysheridan.co.uk
>  
>  
>     
> 
>  
> To avoid confusion, rather than something like
> "$1{$id}$3"
> Which looks really indecipherable, I'd definitely think something like
> '$1' . $id . '$3'
> is a lot easier to read and understand what's going on by immediately
> looking at it.
>  
> As an added bonus, it'll definitely work ;)
>  
>  
>   
> 



-- 
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