Cleansing with preg_replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randell D.

    #1

    Cleansing with preg_replace


    Folks,

    I have a string that *could* contain any character - I want it to call a
    function that will allow me to pass the variable, and then return it cleaned
    of anything except alphanumeric, dots, dashes and commas... How would I do
    this? I have a feeling preg_replace is what I need to use but regular
    expressions have always been over my head. I do know the syntax though
    (taken from a previous posting made some weeks ago) that I've been trying to
    play with - the previous post had

    check to see if a forename field entry is composed only of letters
    if(!preg_match( "/^[a-zA-Z]+$/",$_POST["Nforename"]))
    {
    // invalid form entry. go back to form
    }

    with /^[0-9]+$/ - only digits 0 - 9 allowed

    with /^[a-zA-Z\s]+$/ - letters and spaces

    with /^[a-zA-Z0-9\.]$/ - letters, numbers and periods


    I've been trying something like
    $myString="abc' de!789()";
    $myCleanedStrin g=preg_replace( "/^[a-zA-Z0-9\.]$/", "/^[a-zA-Z0-9\.]$/",
    $myString);

    (I thought it would search characters listed in the frist arguement and
    replace them with characters in the second arguement and ignore everything
    else - but I was wrong).

    Can someone help me here?

    Thanks!


  • Randell D.

    #2
    Re: Cleansing with preg_replace


    "CC Zona" <cczona@nospam. invalid> wrote in message
    news:cczona-3C1FC3.21144017 072003@netnews. attbi.com...[color=blue]
    > In article <kYJRa.451257$3 C2.12305819@new s3.calgary.shaw .ca>,
    > "Randell D." <you.can.email. me.at.randelld@ yahoo.com> wrote:
    >[color=green]
    > > I have a string that *could* contain any character - I want it to call a
    > > function that will allow me to pass the variable, and then return it[/color][/color]
    cleaned[color=blue][color=green]
    > > of anything except alphanumeric, dots, dashes and commas...[/color]
    >
    > $cleaned= preg_replace("/[^a-z\d.,-]/i",'',$strin g);
    >
    > (If you want to keep all whitespace characters too, add "\s"--no[/color]
    quotes--to[color=blue]
    > the class)
    >
    > --
    > CC[/color]

    Thanx


    Comment

    Working...