What's wrong with my function?

Discussion in 'PHP' started by adamjblakey, Sep 10, 2008.

  1. #1
    Hi,

    I have this function:

    
    
    function safe($data) {
    	   $data = str_replace("\r", '', $data);
    	   $data = str_replace("\n", '', $data);
               $data = str_replace("\t", ' ', $data);
               $data = str_replace('’', '\'', $data);
               $data = str_replace('‘', '\'', $data);
               $data = str_replace('“', '"', $data);
               $data = str_replace('&', '&', $data);
               $data = str_replace('£', '£', $data);
               $data = strtr($data, "¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ", "YuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy");
    		   
    		   return $data;
    
    }
    
    
    PHP:
    Which is to make content safe on return and strip out anything i don't want. But it is not taking out the /r or /n and i don't know why.

    This is the text which i am trying it on.

    
    <strong>Lorem Ipsum</strong> is simply dummy text of the printing and\r\ntypesetting industry. Lorem Ipsum has been the industry\'s standard\r\ndummy text ever since the 1500s, when an unknown printer took a galley\r\nof type and scrambled it to make <br><br>a type specimen book. It has survived\r\nnot only five centuries, but also the leap into electronic typesetting,\r\nremaining essentially unchanged. It was popularised in the 1960s with\r\nthe release of Letraset sheets containing Lorem Ipsum passages, and\r\nmore recently with desktop publishing software like Aldus PageMaker\r\nincluding versions of Lorem Ipsum.
    
    Does anyone have any ideas why this is not working?
    
    Cheers,
    Adam
    
    Code (markup):
     
    adamjblakey, Sep 10, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    If you LITERALLY want to remove \r and \n, then you have to put these between single quotes.
    
    $data = str_replace('\r', '', $data);
    $data = str_replace('\n', '', $data);
    $data = str_replace('\t', ' ', $data);
    
    PHP:
    \n between double quotes means a new line character. Which is the same as hitting the enter button when you're editing the source code. The (real) new line character and the graphical representation \n are NOT the same.
     
    nico_swd, Sep 10, 2008 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Cheers nico, works fine now :)
     
    adamjblakey, Sep 10, 2008 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    Just for efficiency, maybe you should do str_replace with arrays.
     
    Kaizoku, Sep 10, 2008 IP