ereg_replace to preg_replace translation

ereg_replace to preg_replace translation

am 11.08.2009 16:34:28 von m a r k u s

Hi all,

I see that from PHP 5.3.0 ereg_replace() function is deprecated and throws a warning.
I would like to use the preg_replace() function as an alternative of ereg_replace() function but...
can't undestand the "\n#[^\n]*\n" expression.

$sql = ereg_replace("\n#[^\n]*\n", "", $sql);

Any help for tranlation or alternative ?
Thanks

--
m a r k u s

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

Re: ereg_replace to preg_replace translation

am 11.08.2009 17:17:28 von Shawn McKenzie

m a r k u s wrote:
> Hi all,
>
> I see that from PHP 5.3.0 ereg_replace() function is deprecated and
> throws a warning.
> I would like to use the preg_replace() function as an alternative of
> ereg_replace() function but... can't undestand the "\n#[^\n]*\n"
> expression.
>
> $sql = ereg_replace("\n#[^\n]*\n", "", $sql);
>
> Any help for tranlation or alternative ?
> Thanks
>
> --
> m a r k u s

Here's a good regex tutorial: http://www.regular-expressions.info/

But to answer your question, \n#[^\n]*\n means the match must:

start with a newline(\n) followed by a pound sign (#) followed by 0 or
more characters (*) that are not a newline(^\n) all the way up to
another newline (\n).

To translate to preg I think all you need to do is give it delimiters,
but that may not be necessary, not sure. "|\n#[^\n]*\n|"

--
Thanks!
-Shawn
http://www.spidean.com

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

RE: ereg_replace to preg_replace translation

am 11.08.2009 17:21:30 von M.Ford

> -----Original Message-----
> From: m a r k u s [mailto:queribus2000@hotmail.com]
> Sent: 11 August 2009 15:34
>=20
> I see that from PHP 5.3.0 ereg_replace() function is deprecated and
> throws a warning.
> I would like to use the preg_replace() function as an alternative of
> ereg_replace() function but...
> can't undestand the "\n#[^\n]*\n" expression.
>=20
> $sql =3D ereg_replace("\n#[^\n]*\n", "", $sql);

Generally the only change you need to make for transition from ereg to preg=
(for simple expressions, anyway) is the addition of pattern delimiters. So=
the above becomes, for example:

$sql =3D preg_replace("|\n#[^\n]*\n|", "", $sql);

Although I would argue that those \ characters should be escaped (and shoul=
d have been even for ereg), so the more correct version of this is:

$sql =3D preg_replace("|\\n#[^\\n]*\\n|", "", $sql);


Cheers!

Mike
--=20
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507, Civic Quarter Campus,=20
Woodhouse Lane, LEEDS,=A0 LS1 3HE,=A0 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