SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ereg_replace help please

    Hello

    Im having trouble with this:
    PHP Code:
    ereg_replace('[\]'""$m_firstn); 
    The $m_firstn var comes from a "First Name" field in a form.

    I need to strip out any "," and " ' " before it is written to a confirm page
    such as "John Doe, Jr" and any apostrophe's.

    The reason Im stripping the "\" in the above is because I later get the the values from a db and anything with " ' " comes back "Lady\'s", so the above seems to take care of that, but Im not sure of the syntax to strip all of above mentioned characters.

    Hope that makes sense.

    Thanks in advance!

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    First, run your string through stripslashes() to remove the escaped quotes.

    Then, you need to assign the results of ereg_replace to a variable, as it returns a string, it doesn't modify its arguments:

    PHP Code:
    $m_firstn stripslashes($m_firstn);
    $m_firstn eregi_replace(","""$m_firstn);
    $m_firstn eregi_replace("'"""$m_firstn); 
    Untested, but you should get the idea.

  3. #3
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah ok, I thought there was a shorthand way to remove both chars in one line.

    That works, Thanks!

  4. #4
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    21
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman
    First, run your string through stripslashes() to remove the escaped quotes.

    Then, you need to assign the results of ereg_replace to a variable, as it returns a string, it doesn't modify its arguments:

    PHP Code:
    $m_firstn stripslashes($m_firstn);
    $m_firstn eregi_replace(","""$m_firstn);
    $m_firstn eregi_replace("'"""$m_firstn); 
    Untested, but you should get the idea.
    str_replace is a lot more efficient.

    $m_firstn = str_replace(",", "", $m_firstn);

    and if you need regular expressions, the preg ones are faster than the ereg ones.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •