replace non-letters with underscore

This is a discussion on replace non-letters with underscore within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I've got the following problem. In a text field people can give their last name. With this input an ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-05-2005
Maarten
 
Posts: n/a
Default replace non-letters with underscore

I've got the following problem.

In a text field people can give their last name.
With this input an pdf file is created.
This works fine until a user has an space or an special character in their
lastname like '.

I would like to do the following:
if a lastname contains other characters than normal letters replace them
with an underscore.
How can I do that?

Maarten


Reply With Quote
  #2 (permalink)  
Old 10-06-2005
Tyrone Slothrop
 
Posts: n/a
Default Re: replace non-letters with underscore

On Wed, 5 Oct 2005 22:53:01 +0200, "Maarten" <rijt@dse.nl> wrote:

>I've got the following problem.
>
>In a text field people can give their last name.
>With this input an pdf file is created.
>This works fine until a user has an space or an special character in their
>lastname like '.
>
>I would like to do the following:
>if a lastname contains other characters than normal letters replace them
>with an underscore.
>How can I do that?
>
>Maarten


You need a regular expression.

preg_replace ("/[^a-zA-Z]/e", "_", $string);
Reply With Quote
  #3 (permalink)  
Old 10-06-2005
Maarten
 
Posts: n/a
Default Re: replace non-letters with underscore

"Tyrone Slothrop" <ts@paranoids.com> schreef in bericht
news:e8u8k1t9oo4sgsrmq8butngnqasqb5lvkr@4ax.com...
> On Wed, 5 Oct 2005 22:53:01 +0200, "Maarten" <rijt@dse.nl> wrote:
>
>>I've got the following problem.
>>
>>In a text field people can give their last name.
>>With this input an pdf file is created.
>>This works fine until a user has an space or an special character in their
>>lastname like '.
>>
>>I would like to do the following:
>>if a lastname contains other characters than normal letters replace them
>>with an underscore.
>>How can I do that?
>>
>>Maarten

>
> You need a regular expression.
>
> preg_replace ("/[^a-zA-Z]/e", "_", $string);


Is the following the same?

preg_replace('\\W', '_', $string);



Reply With Quote
  #4 (permalink)  
Old 10-06-2005
Fabian Hore
 
Posts: n/a
Default Re: replace non-letters with underscore

preg_replace('\\W', '_', $string);
- is invalid -

preg_replace ("/[^a-zA-Z]/e", "_", $string);
- doesn't actually require the "e" -

I would do:
preg_replace('/\W/', '_', $string)







"Maarten" <rijt@dse.nl> wrote in message
news:43452c5f$0$9516$e4fe514c@dreader31.news.xs4al l.nl...
> "Tyrone Slothrop" <ts@paranoids.com> schreef in bericht
> news:e8u8k1t9oo4sgsrmq8butngnqasqb5lvkr@4ax.com...
>> On Wed, 5 Oct 2005 22:53:01 +0200, "Maarten" <rijt@dse.nl> wrote:
>>
>>>I've got the following problem.
>>>
>>>In a text field people can give their last name.
>>>With this input an pdf file is created.
>>>This works fine until a user has an space or an special character in
>>>their
>>>lastname like '.
>>>
>>>I would like to do the following:
>>>if a lastname contains other characters than normal letters replace them
>>>with an underscore.
>>>How can I do that?
>>>
>>>Maarten

>>
>> You need a regular expression.
>>
>> preg_replace ("/[^a-zA-Z]/e", "_", $string);

>
> Is the following the same?
>
> preg_replace('\\W', '_', $string);
>
>
>



Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:01 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0