Replace in a string with regex

Replace in a string with regex

am 22.07.2009 14:02:23 von rszeus

------=_NextPart_000_0142_01CA0ACC.A8744A00
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hello,

I=92m tryng to make some replacements on a string.

Everything go=EAs fine until the regular expression.

=20

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

echo $a =3D str_replace(array(7a45gfdi6icpan1jtb1j99o925,
'temp/',=92_([0-9])=92), array(=93test=94,"",””), $file)

=20

The idea is to remove /temp and the last _1 from the file name..but =
i=92m only
getting this:

screens/test_1_main.jpg

=20

I want it to be: screens/test_main.jpg

=20

Thank you

=20


------=_NextPart_000_0142_01CA0ACC.A8744A00--

Re: Replace in a string with regex

am 22.07.2009 14:11:03 von Eddie Drapkin

On Wed, Jul 22, 2009 at 8:02 AM, rszeus wrote:
> Hello,
>
> Iâ€=99m tryng to make some replacements on a string.
>
> Everything goês fine until the regular expression.
>
>
>
> $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
>
> echo $a =3D  str_replace(array(7a45gfdi6icpan1jtb1j99o925,
> 'temp/',â€=99_([0-9])â€=99), array(â€=9Ctestâ€=9D,"",=E2=
€â€=9D), $file)
>
>
>
> The idea is to remove /temp and the last _1 from the file name..but i=E2=
€™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:


$file =3D "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

Re: Replace in a string with regex

am 22.07.2009 14:20:22 von Ashley Sheridan

On Wed, 2009-07-22 at 13:02 +0100, rszeus wrote:
> Hello,
>=20
> Iâ€=99m tryng to make some replacements on a string.
>=20
> Everything goês fine until the regular expression.
>=20
> =20
>=20
> $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
>=20
> echo $a =3D str_replace(array(7a45gfdi6icpan1jtb1j99o925,
> 'temp/',â€=99_([0-9])â€=99), array(â€=9Ctestâ€=9D,"",=E2=
€â€=9D), $file)
>=20
> =20
>=20
> The idea is to remove /temp and the last _1 from the file name..but i=E2=
€™m only
> getting this:
>=20
> screens/test_1_main.jpg
>=20
> =20
>=20
> I want it to be: screens/test_main.jpg
>=20
> =20
>=20
> Thank you
>=20
> =20
>=20
Well, you seem to have some problems with the syntax you're using on
str_replace(). According to your script, you want to remove the
following:

7a45gfdi6icpan1jtb1j99o925 - which you haven't even enclosed in quote
marks
'temp/'
â€=99_([0-9])â€=99 - which are using weird back ticks, not q=
uote marks

and you are trying to replace those three things with

â€=9Ctestâ€=9D - again, not proper quote marks
""
”” - not proper quote marks

Afaik, str_replace() doesn't allow for text replacement using regular
expressions, for that you'd have to use something like preg_replace()

Also, make sure your strings are correctly enclosed in quote marks, and
that the quote marks you use are actual quote marks and not the accented
'pretty' ones that are offered up by word processors, etc.

A regex which would do the job would look something like this:

^([^/]+)[^_]+\/(.+)$

That should create 2 matches, one for the initial directory and the
other for the latter part of the filename.

Thanks
Ash
www.ashleysheridan.co.uk


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

RE: Replace in a string with regex

am 22.07.2009 15:07:57 von rszeus

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

Thank you

-----Mensagem original-----
De: Eddie Drapkin [mailto:oorza2k5@gmail.com]=20
Enviada: quarta-feira, 22 de Julho de 2009 13:11
Para: rszeus
Cc: php-general@lists.php.net
Assunto: Re: [PHP] Replace in a string with regex

On Wed, Jul 22, 2009 at 8:02 AM, rszeus wrote:
> Hello,
>
> Iâ€=99m tryng to make some replacements on a string.
>
> Everything goês fine until the regular expression.
>
>
>
> $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
>
> echo $a =3D str_replace(array(7a45gfdi6icpan1jtb1j99o925,
> 'temp/',â€=99_([0-9])â€=99), =
array(â€=9Ctestâ€=9D,"",””), $file)
>
>
>
> The idea is to remove /temp and the last _1 from the file name..but =
iâ€=99m 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:


$file =3D "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

Re: Replace in a string with regex

am 22.07.2009 15:11:42 von Eddie Drapkin

On Wed, Jul 22, 2009 at 9:07 AM, rszeus wrote:
> Hi. It Works to remove the _1 but it doesn't replace '7a45gfdi6icpan1jtb1=
j99o925' for 'test'
>
> Thank you
>
> -----Mensagem original-----
> De: Eddie Drapkin [mailto:oorza2k5@gmail.com]
> Enviada: quarta-feira, 22 de Julho de 2009 13:11
> Para: rszeus
> Cc: php-general@lists.php.net
> Assunto: Re: [PHP] Replace in a string with regex
>
> On Wed, Jul 22, 2009 at 8:02 AM, rszeus wrote:
>> Hello,
>>
>> Iâ€=99m tryng to make some replacements on a string.
>>
>> Everything goês fine until the regular expression.
>>
>>
>>
>> $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
>>
>> echo $a =3D  str_replace(array(7a45gfdi6icpan1jtb1j99o925,
>> 'temp/',â€=99_([0-9])â€=99), array(â€=9Ctestâ€=9D,"",=E2=
€â€=9D), $file)
>>
>>
>>
>> The idea is to remove /temp and the last _1 from the file name..but i=E2=
€™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:
>
> >
> $file =3D "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.

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

RE: Replace in a string with regex

am 22.07.2009 15:12:28 von rszeus

Totally right on the corrections. Sorry, i did not copy paste from the =
source code, I wrote here to change to other names and wrote it bad.
It should be:
$file =3D 'screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg';

echo $a =3D str_replace(array('7a45gfdi6icpan1jtb1j99o925', =
'temp/',â€=99_([0-9])â€=99), array('test','',''), $file)

Already understand that str_replace doesn't work with regex, but not =
getting any luck making one preg_replace() to my needs.

Thank you

-----Mensagem original-----
De: Ashley Sheridan [mailto:ash@ashleysheridan.co.uk]=20
Enviada: quarta-feira, 22 de Julho de 2009 13:20
Para: rszeus
Cc: php-general@lists.php.net
Assunto: Re: [PHP] Replace in a string with regex

On Wed, 2009-07-22 at 13:02 +0100, rszeus wrote:
> Hello,
>=20
> Iâ€=99m tryng to make some replacements on a string.
>=20
> Everything goês fine until the regular expression.
>=20
> =20
>=20
> $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
>=20
> echo $a =3D str_replace(array(7a45gfdi6icpan1jtb1j99o925,
> 'temp/',â€=99_([0-9])â€=99), =
array(â€=9Ctestâ€=9D,"",””), $file)
>=20
> =20
>=20
> The idea is to remove /temp and the last _1 from the file name..but =
iâ€=99m only
> getting this:
>=20
> screens/test_1_main.jpg
>=20
> =20
>=20
> I want it to be: screens/test_main.jpg
>=20
> =20
>=20
> Thank you
>=20
> =20
>=20
Well, you seem to have some problems with the syntax you're using on
str_replace(). According to your script, you want to remove the
following:

7a45gfdi6icpan1jtb1j99o925 - which you haven't even enclosed in quote
marks
'temp/'
â€=99_([0-9])â€=99 - which are using weird back ticks, =
not quote marks

and you are trying to replace those three things with

â€=9Ctestâ€=9D - again, not proper quote marks
""
”” - not proper quote marks

Afaik, str_replace() doesn't allow for text replacement using regular
expressions, for that you'd have to use something like preg_replace()

Also, make sure your strings are correctly enclosed in quote marks, and
that the quote marks you use are actual quote marks and not the accented
'pretty' ones that are offered up by word processors, etc.

A regex which would do the job would look something like this:

^([^/]+)[^_]+\/(.+)$

That should create 2 matches, one for the initial directory and the
other for the latter part of the filename.

Thanks
Ash
www.ashleysheridan.co.uk


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

Re: Replace in a string with regex

am 22.07.2009 15:45:32 von Robert Cummings

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:


$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

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

Re: Replace in a string with regex

am 22.07.2009 15:50:17 von Robert Cummings

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

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

RE: Replace in a string with regex

am 22.07.2009 15:58:40 von rszeus

Thank you.

What about instead test i want to insert a variable ?
Like=20
$id =3D "30";
$file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
echo preg_replace('#(screens/)temp/(.+?)_1(_main\.jpg)#', '$1$id$3', =
$file);
I am confusing " and '.=20

Thank you
=20

-----Mensagem original-----
De: Eddie Drapkin [mailto:oorza2k5@gmail.com]=20
Enviada: quarta-feira, 22 de Julho de 2009 14:12
Para: rszeus
Cc: php-general@lists.php.net
Assunto: Re: [PHP] Replace in a string with regex

On Wed, Jul 22, 2009 at 9:07 AM, rszeus 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@gmail.com]
> Enviada: quarta-feira, 22 de Julho de 2009 13:11
> Para: rszeus
> Cc: php-general@lists.php.net
> Assunto: Re: [PHP] Replace in a string with regex
>
> On Wed, Jul 22, 2009 at 8:02 AM, rszeus wrote:
>> Hello,
>>
>> Iâ€=99m tryng to make some replacements on a string.
>>
>> Everything goês fine until the regular expression.
>>
>>
>>
>> $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
>>
>> echo $a =3D str_replace(array(7a45gfdi6icpan1jtb1j99o925,
>> 'temp/',â€=99_([0-9])â€=99), =
array(â€=9Ctestâ€=9D,"",””), $file)
>>
>>
>>
>> The idea is to remove /temp and the last _1 from the file name..but =
iâ€=99m 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:
>
> >
> $file =3D "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.


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

Re: Replace in a string with regex

am 22.07.2009 16:54:49 von List Manager

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@gmail.com]
> Enviada: quarta-feira, 22 de Julho de 2009 14:12
> Para: rszeus
> Cc: php-general@lists.php.net
> Assunto: Re: [PHP] Replace in a string with regex
>
> On Wed, Jul 22, 2009 at 9:07 AM, rszeus 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@gmail.com]
>> Enviada: quarta-feira, 22 de Julho de 2009 13:11
>> Para: rszeus
>> Cc: php-general@lists.php.net
>> Assunto: Re: [PHP] Replace in a string with regex
>>
>> On Wed, Jul 22, 2009 at 8:02 AM, 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
>>>
>>>
>>>
>>> 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:
>>
>> >>
>> $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.
>
>



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

Re: Replace in a string with regex

am 22.07.2009 17:01:37 von Ashley Sheridan

On Wed, 2009-07-22 at 07:54 -0700, Jim Lucas wrote:
> rszeus wrote:
> > Thank you.
> >=20
> > What about instead test i want to insert a variable ?
> > Like=20
> > $id =3D "30";
> > $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
> > echo preg_replace('#(screens/)temp/(.+?)_1(_main\.jpg)#', '$1$id$3', $f=
ile);
>=20
> 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.
>=20
> Then I would surround YOUR variable with curly brackets to separate it
> from the rest.
>=20
> echo preg_replace('#(screens/)temp/.+?_1(_main\.jpg)#',
> "$1{$id}$3",
> $file);
>=20
> > I am confusing " and '.=20
> >=20
> > Thank you
> > =20
> >=20
> > -----Mensagem original-----
> > De: Eddie Drapkin [mailto:oorza2k5@gmail.com]=20
> > Enviada: quarta-feira, 22 de Julho de 2009 14:12
> > Para: rszeus
> > Cc: php-general@lists.php.net
> > Assunto: Re: [PHP] Replace in a string with regex
> >=20
> > On Wed, Jul 22, 2009 at 9:07 AM, rszeus wrote:
> >> Hi. It Works to remove the _1 but it doesn't replace '7a45gfdi6icpan1j=
tb1j99o925' for 'test'
> >>
> >> Thank you
> >>
> >> -----Mensagem original-----
> >> De: Eddie Drapkin [mailto:oorza2k5@gmail.com]
> >> Enviada: quarta-feira, 22 de Julho de 2009 13:11
> >> Para: rszeus
> >> Cc: php-general@lists.php.net
> >> Assunto: Re: [PHP] Replace in a string with regex
> >>
> >> On Wed, Jul 22, 2009 at 8:02 AM, rszeus wrote:
> >>> Hello,
> >>>
> >>> Iâ€=99m tryng to make some replacements on a string.
> >>>
> >>> Everything goês fine until the regular expression.
> >>>
> >>>
> >>>
> >>> $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
> >>>
> >>> echo $a =3D str_replace(array(7a45gfdi6icpan1jtb1j99o925,
> >>> 'temp/',â€=99_([0-9])â€=99), array(â€=9Ctestâ€=9D,"",=
””), $file)
> >>>
> >>>
> >>>
> >>> The idea is to remove /temp and the last _1 from the file name..but i=
â€=99m 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:
> >>
> >> > >>
> >> $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
> >>
> >> echo preg_replace('#(screens/)temp/(.+?)_1(_main\.jpg)#', '$1$2$3', $f=
ile);
> >>
> >>
> >=20
> > In the second parameter, $2 is the string you'd want to replace to
> > test so change '$1$2$3' to '$1test$3'.
> >=20
> > It seems like you're having trouble with regular expressions, may I
> > suggest you read up on them?
> >=20
> > http://www.regular-expressions.info/ is a pretty great free resource,
> > as ridiculous as the design is.
> >=20
> >=20
>=20
>=20
>=20
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


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

Re: Replace in a string with regex

am 22.07.2009 17:03:13 von Eddie Drapkin

On Wed, Jul 22, 2009 at 11:01 AM, Ashley
Sheridan 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 =3D "30";
>> > $file =3D "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 argu=
ment
>> 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@gmail.com]
>> > Enviada: quarta-feira, 22 de Julho de 2009 14:12
>> > Para: rszeus
>> > Cc: php-general@lists.php.net
>> > Assunto: Re: [PHP] Replace in a string with regex
>> >
>> > On Wed, Jul 22, 2009 at 9:07 AM, rszeus wrote:
>> >> Hi. It Works to remove the _1 but it doesn't replace '7a45gfdi6icpan1=
jtb1j99o925' for 'test'
>> >>
>> >> Thank you
>> >>
>> >> -----Mensagem original-----
>> >> De: Eddie Drapkin [mailto:oorza2k5@gmail.com]
>> >> Enviada: quarta-feira, 22 de Julho de 2009 13:11
>> >> Para: rszeus
>> >> Cc: php-general@lists.php.net
>> >> Assunto: Re: [PHP] Replace in a string with regex
>> >>
>> >> On Wed, Jul 22, 2009 at 8:02 AM, rszeus wrote:
>> >>> Hello,
>> >>>
>> >>> Iâ€=99m tryng to make some replacements on a string.
>> >>>
>> >>> Everything goês fine until the regular expression.
>> >>>
>> >>>
>> >>>
>> >>> $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
>> >>>
>> >>> echo $a =3D  str_replace(array(7a45gfdi6icpan1jtb1j99o925,
>> >>> 'temp/',â€=99_([0-9])â€=99), array(â€=9Ctestâ€=9D,""=
,””), $file)
>> >>>
>> >>>
>> >>>
>> >>> The idea is to remove /temp and the last _1 from the file name..but =
iâ€=99m 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:
>> >>
>> >> >> >>
>> >> $file =3D "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

Re: Replace in a string with regex

am 22.07.2009 17:05:36 von Andrew Ballard

On Wed, Jul 22, 2009 at 11:01 AM, Ashley
Sheridan wrote:
> On Wed, 2009-07-22 at 07:54 -0700, Jim Lucas wrote:
>> Sure that can be done.  But you will need to change the second argu=
ment
>> 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 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

Don't you also have to escape the $'s for the matches since it's in
double quotes?

"\$1{$id}\$3"

Andrew

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

RE: Replace in a string with regex

am 22.07.2009 18:19:01 von rszeus

Hi,=20
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@gmail.com]=20
Enviada: quarta-feira, 22 de Julho de 2009 16:03
Para: ash@ashleysheridan.co.uk
Cc: Jim Lucas; rszeus; php-general@lists.php.net
Assunto: Re: [PHP] Replace in a string with regex

On Wed, Jul 22, 2009 at 11:01 AM, Ashley
Sheridan 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 =3D "30";
>> > $file =3D "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@gmail.com]
>> > Enviada: quarta-feira, 22 de Julho de 2009 14:12
>> > Para: rszeus
>> > Cc: php-general@lists.php.net
>> > Assunto: Re: [PHP] Replace in a string with regex
>> >
>> > On Wed, Jul 22, 2009 at 9:07 AM, rszeus 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@gmail.com]
>> >> Enviada: quarta-feira, 22 de Julho de 2009 13:11
>> >> Para: rszeus
>> >> Cc: php-general@lists.php.net
>> >> Assunto: Re: [PHP] Replace in a string with regex
>> >>
>> >> On Wed, Jul 22, 2009 at 8:02 AM, rszeus wrote:
>> >>> Hello,
>> >>>
>> >>> Iâ€=99m tryng to make some replacements on a string.
>> >>>
>> >>> Everything goês fine until the regular expression.
>> >>>
>> >>>
>> >>>
>> >>> $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
>> >>>
>> >>> echo $a =3D str_replace(array(7a45gfdi6icpan1jtb1j99o925,
>> >>> 'temp/',â€=99_([0-9])â€=99), =
array(â€=9Ctestâ€=9D,"",””), $file)
>> >>>
>> >>>
>> >>>
>> >>> The idea is to remove /temp and the last _1 from the file =
name..but iâ€=99m 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:
>> >>
>> >> >> >>
>> >> $file =3D "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

Re: Replace in a string with regex

am 22.07.2009 18:21:54 von kyle.smith

--------------080305040008000903010709
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit

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@gmail.com]
> Enviada: quarta-feira, 22 de Julho de 2009 16:03
> Para: ash@ashleysheridan.co.uk
> Cc: Jim Lucas; rszeus; php-general@lists.php.net
> Assunto: Re: [PHP] Replace in a string with regex
>
> On Wed, Jul 22, 2009 at 11:01 AM, Ashley
> Sheridan 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@gmail.com]
>>>> Enviada: quarta-feira, 22 de Julho de 2009 14:12
>>>> Para: rszeus
>>>> Cc: php-general@lists.php.net
>>>> Assunto: Re: [PHP] Replace in a string with regex
>>>>
>>>> On Wed, Jul 22, 2009 at 9:07 AM, rszeus 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@gmail.com]
>>>>> Enviada: quarta-feira, 22 de Julho de 2009 13:11
>>>>> Para: rszeus
>>>>> Cc: php-general@lists.php.net
>>>>> Assunto: Re: [PHP] Replace in a string with regex
>>>>>
>>>>> On Wed, Jul 22, 2009 at 8:02 AM, 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
>>>>>>
>>>>>>
>>>>>>
>>>>>> 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:
>>>>>
>>>>> >>>>>
>>>>> $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 ;)
>
>
>

--------------080305040008000903010709--

RE: Replace in a string with regex

am 22.07.2009 19:42:21 von rszeus

------=_NextPart_000_0169_01CA0AFC.2723E2A0
Content-Type: text/plain;
charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Thank you. I undestand now.

Anh itâ€=99s already workyng the replacemente with letteres. Bu if =
the variabele is a number it doensâ€=99t work. Any ideas ?

=20

$file =3D "screen/temp/7a45gfdi6icpan1jtb1j99o925_1_mini.jpg";

=20

$id =3D '70';

echo preg_replace('#(Iscreen/)temp/(.+?)_1(.+?\.jpg)#', '$1'.$id, =
$file);

I get 0

=20

=20

$id =3D 'test';

echo preg_replace('#(screen/)temp/(.+?)_1(.+?\.jpg)#', '$1'.$id, =
$file);

I get screen/test

=20

Any ideas ?

=20

Thank you

=20

De: Kyle Smith [mailto:kyle.smith@inforonics.com]=20
Enviada: quarta-feira, 22 de Julho de 2009 17:22
Para: rszeus
Cc: 'Eddie Drapkin'; ash@ashleysheridan.co.uk; 'Jim Lucas'; =
php-general@lists.php.net
Assunto: Re: [PHP] Replace in a string with regex

=20

The first match inside () is assigned to $1, the second is assigned to =
$2, and so on.

rszeus wrote:=20

Hi,=20
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.
=20
Thank you...
=20
-----Mensagem original-----
De: Eddie Drapkin [mailto:oorza2k5@gmail.com]=20
Enviada: quarta-feira, 22 de Julho de 2009 16:03
Para: ash@ashleysheridan.co.uk
Cc: Jim Lucas; rszeus; php-general@lists.php.net
Assunto: Re: [PHP] Replace in a string with regex
=20
On Wed, Jul 22, 2009 at 11:01 AM, Ashley
Sheridan =
wrote:
=20

On Wed, 2009-07-22 at 07:54 -0700, Jim Lucas wrote:
=20

rszeus wrote:
=20

Thank you.
=20
What about instead test i want to insert a variable ?
Like
$id =3D "30";
$file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
echo preg_replace('#(screens/)temp/(.+?)_1(_main\.jpg)#', '$1$id$3', =
$file);
=20

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.
=20
Then I would surround YOUR variable with curly brackets to separate it
from the rest.
=20
echo preg_replace('#(screens/)temp/.+?_1(_main\.jpg)#',
"$1{$id}$3",
$file);
=20
=20

I am confusing " and '.
=20
Thank you
=20
=20
-----Mensagem original-----
De: Eddie Drapkin [mailto:oorza2k5@gmail.com]
Enviada: quarta-feira, 22 de Julho de 2009 14:12
Para: rszeus
Cc: php-general@lists.php.net
Assunto: Re: [PHP] Replace in a string with regex
=20
On Wed, Jul 22, 2009 at 9:07 AM, rszeus =
wrote:
=20

Hi. It Works to remove the _1 but it doesn't replace =
'7a45gfdi6icpan1jtb1j99o925' for 'test'
=20
Thank you
=20
-----Mensagem original-----
De: Eddie Drapkin [mailto:oorza2k5@gmail.com]
Enviada: quarta-feira, 22 de Julho de 2009 13:11
Para: rszeus
Cc: php-general@lists.php.net
Assunto: Re: [PHP] Replace in a string with regex
=20
On Wed, Jul 22, 2009 at 8:02 AM, rszeus =
wrote:
=20

Hello,
=20
Iâ€=99m tryng to make some replacements on a string.
=20
Everything goês fine until the regular expression.
=20
=20
=20
$file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
=20
echo $a =3D str_replace(array(7a45gfdi6icpan1jtb1j99o925,
'temp/',â€=99_([0-9])â€=99), =
array(â€=9Ctestâ€=9D,"",””), $file)
=20
=20
=20
The idea is to remove /temp and the last _1 from the file name..but =
iâ€=99m only
getting this:
=20
screens/test_1_main.jpg
=20
=20
=20
I want it to be: screens/test_main.jpg
=20
=20
=20
Thank you
=20
=20
=20
=20
=20

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.
=20
Try this one out, I think I got what you wanted to do:
=20
=20
$file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
=20
echo preg_replace('#(screens/)temp/(.+?)_1(_main\.jpg)#', '$1$2$3', =
$file);
=20
=20
=20

In the second parameter, $2 is the string you'd want to replace to
test so change '$1$2$3' to '$1test$3'.
=20
It seems like you're having trouble with regular expressions, may I
suggest you read up on them?
=20
http://www.regular-expressions.info/ is a pretty great free resource,
as ridiculous as the design is.
=20
=20
=20

=20
=20
=20

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?
=20
Thanks
Ash
www.ashleysheridan.co.uk
=20
=20
=20

=20
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.
=20
As an added bonus, it'll definitely work ;)
=20
=20
=20

------=_NextPart_000_0169_01CA0AFC.2723E2A0--

Re: Replace in a string with regex

am 22.07.2009 19:44:26 von List Manager

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@inforonics.com]
> Enviada: quarta-feira, 22 de Julho de 2009 17:22
> Para: rszeus
> Cc: 'Eddie Drapkin'; ash@ashleysheridan.co.uk; 'Jim Lucas'; php-general@lists.php.net
> Assunto: Re: [PHP] 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@gmail.com]
> Enviada: quarta-feira, 22 de Julho de 2009 16:03
> Para: ash@ashleysheridan.co.uk
> Cc: Jim Lucas; rszeus; php-general@lists.php.net
> Assunto: Re: [PHP] Replace in a string with regex
>
> On Wed, Jul 22, 2009 at 11:01 AM, Ashley
> Sheridan 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@gmail.com]
> Enviada: quarta-feira, 22 de Julho de 2009 14:12
> Para: rszeus
> Cc: php-general@lists.php.net
> Assunto: Re: [PHP] Replace in a string with regex
>
> On Wed, Jul 22, 2009 at 9:07 AM, rszeus 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@gmail.com]
> Enviada: quarta-feira, 22 de Julho de 2009 13:11
> Para: rszeus
> Cc: php-general@lists.php.net
> Assunto: Re: [PHP] Replace in a string with regex
>
> On Wed, Jul 22, 2009 at 8:02 AM, 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
>
>
>
> 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:
>
> >
> $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

RE: Replace in a string with regex

am 22.07.2009 20:23:27 von rszeus

No, sory, my bad typing. It's not the problem..
I have the same on both caxses, only chnage the variable $id=20
$file =3D "screen/temp/7a45gfdi6icpan1jtb1j99o925_1_mini.jpg"=20
$id =3D '70';
echo preg_replace('#(screen/)temp/(.+?)_1(.+?\.jpg)#', '$1'.$id, =
$file);
Get: 0
=20
file =3D "screen/temp/7a45gfdi6icpan1jtb1j99o925_1_mini.jpg";
$id =3D 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@cmsws.com]=20
Enviada: quarta-feira, 22 de Julho de 2009 18:44
Para: rszeus
Cc: 'Kyle Smith'; 'Eddie Drapkin'; ash@ashleysheridan.co.uk; =
php-general@lists.php.net
Assunto: Re: [PHP] Replace in a string with regex

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

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


> I get 0
>=20
> =20
>=20
> =20
>=20
> $id =3D 'test';
>=20
> echo preg_replace('#(screen/)temp/(.+?)_1(.+?\.jpg)#', '$1'.$id, =
$file);
>=20
> I get screen/test
>=20
> =20
>=20
> Any ideas ?
>=20
> =20
>=20
> Thank you
>=20
> =20
>=20
> De: Kyle Smith [mailto:kyle.smith@inforonics.com]=20
> Enviada: quarta-feira, 22 de Julho de 2009 17:22
> Para: rszeus
> Cc: 'Eddie Drapkin'; ash@ashleysheridan.co.uk; 'Jim Lucas'; =
php-general@lists.php.net
> Assunto: Re: [PHP] Replace in a string with regex
>=20
> =20
>=20
> The first match inside () is assigned to $1, the second is assigned to =
$2, and so on.
>=20
> rszeus wrote:=20
>=20
> Hi,=20
> 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.
> =20
> Thank you...
> =20
> -----Mensagem original-----
> De: Eddie Drapkin [mailto:oorza2k5@gmail.com]=20
> Enviada: quarta-feira, 22 de Julho de 2009 16:03
> Para: ash@ashleysheridan.co.uk
> Cc: Jim Lucas; rszeus; php-general@lists.php.net
> Assunto: Re: [PHP] Replace in a string with regex
> =20
> On Wed, Jul 22, 2009 at 11:01 AM, Ashley
> Sheridan =
wrote:
> =20
>=20
> On Wed, 2009-07-22 at 07:54 -0700, Jim Lucas wrote:
> =20
>=20
> rszeus wrote:
> =20
>=20
> Thank you.
> =20
> What about instead test i want to insert a variable ?
> Like
> $id =3D "30";
> $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
> echo preg_replace('#(screens/)temp/(.+?)_1(_main\.jpg)#', '$1$id$3', =
$file);
> =20
>=20
> 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.
> =20
> Then I would surround YOUR variable with curly brackets to separate it
> from the rest.
> =20
> echo preg_replace('#(screens/)temp/.+?_1(_main\.jpg)#',
> "$1{$id}$3",
> $file);
> =20
> =20
>=20
> I am confusing " and '.
> =20
> Thank you
> =20
> =20
> -----Mensagem original-----
> De: Eddie Drapkin [mailto:oorza2k5@gmail.com]
> Enviada: quarta-feira, 22 de Julho de 2009 14:12
> Para: rszeus
> Cc: php-general@lists.php.net
> Assunto: Re: [PHP] Replace in a string with regex
> =20
> On Wed, Jul 22, 2009 at 9:07 AM, rszeus =
wrote:
> =20
>=20
> Hi. It Works to remove the _1 but it doesn't replace =
'7a45gfdi6icpan1jtb1j99o925' for 'test'
> =20
> Thank you
> =20
> -----Mensagem original-----
> De: Eddie Drapkin [mailto:oorza2k5@gmail.com]
> Enviada: quarta-feira, 22 de Julho de 2009 13:11
> Para: rszeus
> Cc: php-general@lists.php.net
> Assunto: Re: [PHP] Replace in a string with regex
> =20
> On Wed, Jul 22, 2009 at 8:02 AM, rszeus =
wrote:
> =20
>=20
> Hello,
> =20
> Iâ€=99m tryng to make some replacements on a string.
> =20
> Everything goês fine until the regular expression.
> =20
> =20
> =20
> $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
> =20
> echo $a =3D str_replace(array(7a45gfdi6icpan1jtb1j99o925,
> 'temp/',â€=99_([0-9])â€=99), =
array(â€=9Ctestâ€=9D,"",””), $file)
> =20
> =20
> =20
> The idea is to remove /temp and the last _1 from the file name..but =
iâ€=99m only
> getting this:
> =20
> screens/test_1_main.jpg
> =20
> =20
> =20
> I want it to be: screens/test_main.jpg
> =20
> =20
> =20
> Thank you
> =20
> =20
> =20
> =20
> =20
>=20
> 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.
> =20
> Try this one out, I think I got what you wanted to do:
> =20
> > =20
> $file =3D "screens/temp/7a45gfdi6icpan1jtb1j99o925_1_main.jpg";
> =20
> echo preg_replace('#(screens/)temp/(.+?)_1(_main\.jpg)#', '$1$2$3', =
$file);
> =20
> =20
> =20
>=20
> In the second parameter, $2 is the string you'd want to replace to
> test so change '$1$2$3' to '$1test$3'.
> =20
> It seems like you're having trouble with regular expressions, may I
> suggest you read up on them?
> =20
> http://www.regular-expressions.info/ is a pretty great free resource,
> as ridiculous as the design is.
> =20
> =20
> =20
>=20
> =20
> =20
> =20
>=20
> 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?
> =20
> Thanks
> Ash
> www.ashleysheridan.co.uk
> =20
> =20
> =20
>=20
> =20
> 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.
> =20
> As an added bonus, it'll definitely work ;)
> =20
> =20
> =20
>=20



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

Re: Replace in a string with regex

am 22.07.2009 20:53:54 von List Manager

rszeus wrote:
> 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;

Well, here you are trying to use a constant called test to set your
variable $id. I would try surrounding test with single or double quotes

> 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@cmsws.com]
> Enviada: quarta-feira, 22 de Julho de 2009 18:44
> Para: rszeus
> Cc: 'Kyle Smith'; 'Eddie Drapkin'; ash@ashleysheridan.co.uk; php-general@lists.php.net
> Assunto: Re: [PHP] 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@inforonics.com]
>> Enviada: quarta-feira, 22 de Julho de 2009 17:22
>> Para: rszeus
>> Cc: 'Eddie Drapkin'; ash@ashleysheridan.co.uk; 'Jim Lucas'; php-general@lists.php.net
>> Assunto: Re: [PHP] 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@gmail.com]
>> Enviada: quarta-feira, 22 de Julho de 2009 16:03
>> Para: ash@ashleysheridan.co.uk
>> Cc: Jim Lucas; rszeus; php-general@lists.php.net
>> Assunto: Re: [PHP] Replace in a string with regex
>>
>> On Wed, Jul 22, 2009 at 11:01 AM, Ashley
>> Sheridan 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@gmail.com]
>> Enviada: quarta-feira, 22 de Julho de 2009 14:12
>> Para: rszeus
>> Cc: php-general@lists.php.net
>> Assunto: Re: [PHP] Replace in a string with regex
>>
>> On Wed, Jul 22, 2009 at 9:07 AM, rszeus 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@gmail.com]
>> Enviada: quarta-feira, 22 de Julho de 2009 13:11
>> Para: rszeus
>> Cc: php-general@lists.php.net
>> Assunto: Re: [PHP] Replace in a string with regex
>>
>> On Wed, Jul 22, 2009 at 8:02 AM, 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
>>
>>
>>
>> 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:
>>
>> >>
>> $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

RE: Replace in a string with regex

am 23.07.2009 01:04:03 von M.Ford

PiAtLS0tLU9yaWdpbmFsIE1lc3NhZ2UtLS0tLQ0KPiBGcm9tOiByc3pldXMg
W21haWx0bzpyc3pldXNAZ21haWwuY29tXQ0KPiBTZW50OiAyMiBKdWx5IDIw
MDkgMTk6MjMNCj4gVG86ICdKaW0gTHVjYXMnDQo+IENjOiAnS3lsZSBTbWl0
aCc7ICdFZGRpZSBEcmFwa2luJzsgYXNoQGFzaGxleXNoZXJpZGFuLmNvLnVr
OyBwaHAtDQo+IGdlbmVyYWxAbGlzdHMucGhwLm5ldA0KPiBTdWJqZWN0OiBS
RTogW1BIUF0gUmVwbGFjZSBpbiBhIHN0cmluZyB3aXRoIHJlZ2V4DQo+IA0K
PiBObywgc29yeSwgbXkgYmFkIHR5cGluZy4gSXQncyBub3QgdGhlIHByb2Js
ZW0uLg0KPiBJIGhhdmUgdGhlIHNhbWUgb24gYm90aCBjYXhzZXMsIG9ubHkg
Y2huYWdlIHRoZSB2YXJpYWJsZSAkaWQNCj4gICRmaWxlID0gInNjcmVlbi90
ZW1wLzdhNDVnZmRpNmljcGFuMWp0YjFqOTlvOTI1XzFfbWluaS5qcGciDQo+
ICAkaWQgPSAnNzAnOw0KPiAgZWNobyBwcmVnX3JlcGxhY2UoJyMoc2NyZWVu
Lyl0ZW1wLyguKz8pXzEoLis/XC5qcGcpIycsICckMScuJGlkLA0KPiAkZmls
ZSk7DQo+ICBHZXQ6IDANCj4gDQo+IGZpbGUgPSAic2NyZWVuL3RlbXAvN2E0
NWdmZGk2aWNwYW4xanRiMWo5OW85MjVfMV9taW5pLmpwZyI7DQo+ICRpZCA9
IHRlc3Q7DQo+IGVjaG8gcHJlZ19yZXBsYWNlKCcjKHNjcmVlbi8pdGVtcC8o
Lis/KV8xKC4rP1wuanBnKSMnLCAnJDEnLiRpZCwNCj4gJGZpbGUpOw0KPiAN
Cj4gR2V0OiBzY3JlZW4vdGVzdA0KPiANCj4gV2hhdCBpcyB3cm9uZyB3aXRo
IGhhdmluZyBuYSBpbnRlZ2VyIG9uIHRoZSB2YXIgPw0KDQpXZWxsLCB0aGUg
cHJvYmxlbSBoZXJlIGlzIHRoYXQgd2hlbiB5b3UgY29uY2F0ZW5hdGUgJGlk
IGNvbnRhaW5pbmcgNzAgb24gdG8gJyQxJywgeW91IGVmZmVjdGl2ZWx5IGVu
ZCB1cCB3aXRoICckMTcwJyAtLSB3aGljaCB0aGUgbWFudWFsIHBhZ2UgYXQg
aHR0cDovL3BocC5uZXQvcHJlZy1yZXBsYWNlIG1ha2VzIGNsZWFyIGlzIGFt
YmlndW91cywgYnV0IGlzIGxpa2VseSB0byBiZSB0cmVhdGVkIGFzICQxNyBm
b2xsb3dlZCBieSBhIHplcm8sIHJhdGhlciB0aGFuICQxIGZvbGxvd2VkIGJ5
IDcwLiBTaW5jZSAkMTcgZG9lc24ndCBleGlzdCAoYXMgeW91IGRvbid0IGhh
dmUgdGhhdCBtYW55IGNhcHR1cmluZyBzdWJwYXR0ZXJucyBpbiB5b3VyIHBh
dHRlcm4hKSwgaXQgaXMgdGFrZW4gdG8gYmUgdGhlIG51bGwgc3RyaW5nIC0t
IGxlYXZpbmcganVzdCB0aGUgbGVmdCBvdmVyIDAgYXMgdGhlIHJlc3VsdCBv
ZiB0aGUgcmVwbGFjZW1lbnQuIFFFRC4NCg0KVGhlIHdvcmthcm91bmQgZm9y
IHRoaXMgaXMgYWxzbyBnaXZlbiBvbiB0aGUgcGFnZSByZWZlcmVuY2VkIGFi
b3ZlLCB3aGljaCBpcyB0byBtYWtlIHlvdXIgcmVwbGFjZW1lbnQgYmUgJyR7
MX0nLiRpZC4NCg0KSW5jaWRlbnRhbGx5LCBJIGRvbid0IHJlYWxseSBzZWUg
dGhlIG5lZWQgZm9yIHRoZSAkMSwgb3IgdGhlIGVxdWl2YWxlbnQgcGFyZW50
aGVzZXMgaW4gdGhlIHBhdHRlcm4gLS0gc2luY2UgYSBmaXhlZCBzdHJpbmcg
aXMgaW52b2x2ZWQsIHdoeSBub3QganVzdCB1c2UgaXQgZGlyZWN0bHkgaW4g
Ym90aCBwbGFjZXM/IExpa2UgdGhpczoNCg0KICAgcHJlZ19yZXBsYWNlKCcj
c2NyZWVuL3RlbXAvKC4rPylfMSguKz9cLmpwZykjJywgJ3NjcmVlbi8nLiRp
ZCwgJGZpbGUpOw0KDQp3aGljaCBjb21wbGV0ZWx5IHNpZGVzdGVwcyB0aGUg
cHJvYmxlbS4NCg0KQ2hlZXJzIQ0KDQpNaWtlDQogLS0gDQpNaWtlIEZvcmQs
DQpFbGVjdHJvbmljIEluZm9ybWF0aW9uIERldmVsb3BlciwgTGlicmFyaWVz
IGFuZCBMZWFybmluZyBJbm5vdmF0aW9uLCAgDQpMZWVkcyBNZXRyb3BvbGl0
YW4gVW5pdmVyc2l0eSwgQzUwNywgQ2l2aWMgUXVhcnRlciBDYW1wdXMsIA0K
V29vZGhvdXNlIExhbmUsIExFRURTLMKgIExTMSAzSEUswqAgVW5pdGVkIEtp
bmdkb20gDQpFbWFpbDogbS5mb3JkQGxlZWRzbWV0LmFjLnVrIA0KVGVsOiAr
NDQgMTEzIDgxMiA0NzMwDQoNCg0KCgpUbyB2aWV3IHRoZSB0ZXJtcyB1bmRl
ciB3aGljaCB0aGlzIGVtYWlsIGlzIGRpc3RyaWJ1dGVkLCBwbGVhc2UgZ28g
dG8gaHR0cDovL2Rpc2NsYWltZXIubGVlZHNtZXQuYWMudWsvZW1haWwuaHRt
Cg==

RE: Replace in a string with regex

am 23.07.2009 01:59:14 von rszeus

Thank you very much!
Understand. And it Works very well now.

Cheers

-----Mensagem original-----
De: Ford, Mike [mailto:M.Ford@leedsmet.ac.uk]=20
Enviada: quinta-feira, 23 de Julho de 2009 00:04
Para: php-general@lists.php.net
Assunto: RE: [PHP] Replace in a string with regex

> -----Original Message-----
> From: rszeus [mailto:rszeus@gmail.com]
> Sent: 22 July 2009 19:23
> To: 'Jim Lucas'
> Cc: 'Kyle Smith'; 'Eddie Drapkin'; ash@ashleysheridan.co.uk; php-
> general@lists.php.net
> Subject: RE: [PHP] Replace in a string with regex
>=20
> No, sory, my bad typing. It's not the problem..
> I have the same on both caxses, only chnage the variable $id
> $file =3D "screen/temp/7a45gfdi6icpan1jtb1j99o925_1_mini.jpg"
> $id =3D '70';
> echo preg_replace('#(screen/)temp/(.+?)_1(.+?\.jpg)#', '$1'.$id,
> $file);
> Get: 0
>=20
> file =3D "screen/temp/7a45gfdi6icpan1jtb1j99o925_1_mini.jpg";
> $id =3D test;
> echo preg_replace('#(screen/)temp/(.+?)_1(.+?\.jpg)#', '$1'.$id,
> $file);
>=20
> Get: screen/test
>=20
> What is wrong with having na integer on the var ?

Well, the problem here is that when you concatenate $id containing 70 on =
to '$1', you effectively end up with '$170' -- which the manual page at =
http://php.net/preg-replace makes clear is ambiguous, but is likely to =
be treated as $17 followed by a zero, rather than $1 followed by 70. =
Since $17 doesn't exist (as you don't have that many capturing =
subpatterns in your pattern!), it is taken to be the null string -- =
leaving just the left over 0 as the result of the replacement. QED.

The workaround for this is also given on the page referenced above, =
which is to make your replacement be '${1}'.$id.

Incidentally, I don't really see the need for the $1, or the equivalent =
parentheses in the pattern -- since a fixed string is involved, why not =
just use it directly in both places? Like this:

preg_replace('#screen/temp/(.+?)_1(.+?\.jpg)#', 'screen/'.$id, =
$file);

which completely sidesteps the problem.

Cheers!

Mike
--=20
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation, =20
Leeds Metropolitan University, C507, Civic Quarter Campus,=20
Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom=20
Email: m.ford@leedsmet.ac.uk=20
Tel: +44 113 812 4730




To view the terms under which this email is distributed, please go to =
http://disclaimer.leedsmet.ac.uk/email.htm


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