From: Rick Dwyer on
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?


--Rick


From: =?iso-8859-2?Q?David_=C8esal?= on
Hello,
can this resolve your problem?

$trans = array(
"from" => "to",
"another" => "to");

$moditem = StrTr($moditem, $trans);

-- http://cz.php.net/manual/en/function.strtr.php

David

-----Original Message-----
From: Rick Dwyer [mailto:rpdwyer(a)earthlink.net]
Sent: Tuesday, June 22, 2010 5:41 PM
To: php-general(a)lists.php.net
Subject: [PHP] Stripping Characters

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?


--Rick



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


From: Ashley Sheridan on
On Tue, 2010-06-22 at 11:40 -0400, Rick Dwyer 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?
>
>
> --Rick
>
>
>


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: Shreyas Agasthya on
Perhaps, ereg_replace("your regex", "replacement_string", String
$variable).

Regards,
Shreyas

On Tue, Jun 22, 2010 at 9:10 PM, 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?
>
>
> --Rick
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Regards,
Shreyas Agasthya
From: Richard Quadling on
On 22 June 2010 16:44, Ashley Sheridan <ash(a)ashleysheridan.co.uk> wrote:
> On Tue, 2010-06-22 at 11:40 -0400, Rick Dwyer 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?
>>
>>
>>   --Rick
>>
>>
>>
>
>
> 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
>
>
>

Watch out for white space in there. Tabs, spaces, new lines, etc. will
also be converted to underscore.

$find = '/[^\w\s]/i';

[^\w\s]

Match a single character NOT present in the list below «[^\w\s]»
A word character (letters, digits, and underscores) «\w»
A whitespace character (spaces, tabs, and line breaks) «\s»


"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