From: Nathan Nobbe on
On Tue, Jun 22, 2010 at 9:40 AM, Rick Dwyer <rpdwyer(a)earthlink.net> wrote:

> Hello List.
>
> I need to remove characters from a string and replace them with and
> underscore.
>
> So instead of having something like:
>
> $moditem = str_replace("--","_","$mystring");
> $moditem = str_replace("?","_","$mystring");
> $moditem = str_replace("!","_","$mystring");
> ....etc.
>
> For every possible character I can think of, is there a way to simply omit
> any character that is not an alpha character and not a number value from 0
> to 9?
>

check the docs, the first parameter may be an array of multiple needles,
e.g.

$moditem = str_replace(array('-', '?', '!'), '_', $mystring);

you could likely do something more elegant w/ preg_replace() tho.

-nathan
From: Richard Quadling on
"A "word" character is any letter or digit or the underscore
character, that is, any character which can be part of a Perl "word".
The definition of letters and digits is controlled by PCRE's character
tables, and may vary if locale-specific matching is taking place. For
example, in the "fr" (French) locale, some character codes greater
than 128 are used for accented letters, and these are matched by \w."

The above becomes ...

_A _word_ character is any letter or digit or the underscore
character_ that is_ any character which can be part of a Perl _word__
The definition of letters and digits is controlled by PCRE_s character
tables_ and may vary if locale_specific matching is taking place_ For
example_ in the _fr_ _French_ locale_ some character codes greater
than 128 are used for accented letters_ and these are matched by _w__

--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling
From: Shreyas Agasthya on
Then, when does one use ereg_replace as against preg_replace? I read from
one the forums that preg_* is faster and ereg_* is if not faster but
simpler.

Is that it?

Regards,
Shreyas



On Tue, Jun 22, 2010 at 9:58 PM, Richard Quadling <rquadling(a)gmail.com>wrote:

> "A "word" character is any letter or digit or the underscore
> character, that is, any character which can be part of a Perl "word".
> The definition of letters and digits is controlled by PCRE's character
> tables, and may vary if locale-specific matching is taking place. For
> example, in the "fr" (French) locale, some character codes greater
> than 128 are used for accented letters, and these are matched by \w."
>
> The above becomes ...
>
> _A _word_ character is any letter or digit or the underscore
> character_ that is_ any character which can be part of a Perl _word__
> The definition of letters and digits is controlled by PCRE_s character
> tables_ and may vary if locale_specific matching is taking place_ For
> example_ in the _fr_ _French_ locale_ some character codes greater
> than 128 are used for accented letters_ and these are matched by _w__
>
> --
> -----
> Richard Quadling
> "Standing on the shoulders of some very clever giants!"
> EE : http://www.experts-exchange.com/M_248814.html
> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
> ZOPA : http://uk.zopa.com/member/RQuadling
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Regards,
Shreyas Agasthya
From: Rick Dwyer on
Thanks to everyone who responded.

Regarding the myriad of choices, isn't Ashley's, listed below, the one
most like to guarantee the cleanest output of just letters and numbers?


--Rick


On Jun 22, 2010, at 11:44 AM, Ashley Sheridan wrote:

> On Tue, 2010-06-22 at 11:40 -0400, Rick Dwyer wrote:
> Use preg_replace(), which allows you to use a regex to specify what
> you want to match:
>
> $find = '/[^a-z0-9]/i';
> $replace = '_';
> $new_string = preg_replace($find, $replace, $old_string);
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>

From: Ashley Sheridan on
On Tue, 2010-06-22 at 13:35 -0400, Rick Dwyer wrote:

> Thanks to everyone who responded.
>
> Regarding the myriad of choices, isn't Ashley's, listed below, the one
> most like to guarantee the cleanest output of just letters and numbers?
>
>
> --Rick
>
>
> On Jun 22, 2010, at 11:44 AM, Ashley Sheridan wrote:
>
> > On Tue, 2010-06-22 at 11:40 -0400, Rick Dwyer wrote:
> > Use preg_replace(), which allows you to use a regex to specify what
> > you want to match:
> >
> > $find = '/[^a-z0-9]/i';
> > $replace = '_';
> > $new_string = preg_replace($find, $replace, $old_string);
> >
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
>


It is clean, but as Richard mentioned, it won't handle strings outside
of the traditional 128 ASCII range, so accented characters and the like
will be converted to an underscore. Also, spaces might become an issue.

However, if you are happy that your input won't go beyond the a-z0-9
range, then it should do what you need.

Thanks,
Ash
http://www.ashleysheridan.co.uk