Remove Quotation Marks from variable

Discussion in 'PHP' started by makamo66, Oct 1, 2013.

  1. #1
    I'm trying to trim away the quotation marks from a php variable in cakephp. I've tried trim(), substr(), ereg_replace(), and str_replace() but nothing affects the quotation marks. When I use substr like this substr($comma_separated,1,-1); it removes the first and the last letter but not the quotation marks. The string is $comma_separated = "','" and this is an invalid email address for CakeEmail. I've also tried $comma_separated = ereg_replace('"', "", $comma_separated); and $comma_separated = str_replace('"', '', $comma_separated);
     
    makamo66, Oct 1, 2013 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    So... you want to remove the single quotation-marks? Ie being left with , ?
    run $comma_separated = str_replace("'",$comma_separated);

    If that doesn't work, you have other problems, or cakephp is doing something weird
     
    PoPSiCLe, Oct 1, 2013 IP
  3. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    I wasn't specific enough. It's the double quotation marks that I'm trying to remove. Not the single ones.
     
    makamo66, Oct 1, 2013 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    both are illegal in an email-address, AFAIK, but okay - first of all, as the content you put forward was as follows:
    $comma_separated = "','" <- that variable does NOT contain any double quotation-marks, neither does it conform to proper formatting.
    a valid variable would be something like this: $comma_separated = "".","."" (which doesn't make sense), or the following:
    $comma_separated = ", "; <- the outer double quotation marks just encapsulated the content of the variable, and are NOT part of the content itself.

    If what you're saying is that the CONTENT of the variable $comma_separated is "','" - then you have another problem completely.

    However, if you have a variable like this: $comma_separated = '"\',\'"' (which would be somewhat correct, for content) you could still use a $comma_separated = str_replace("\"",$comma_separated);

    if you escape the double quotation, it should work, but you need to take a better look at the data you're getting, and feeding into the variable, because somewhere, there's a fuckup
     
    PoPSiCLe, Oct 1, 2013 IP
  5. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    I've tried all this and they look like the normal quotation marks
    $comma_separated = ereg_replace('"', "", $comma_separated); //no change in output
    $comma_separated = str_replace('"', '', $comma_separated); //no change in output
    $comma_separated = substr($comma_separated,1,-1); //outputs the last and first letter removed
    $comma_separated = trim($comma_separated,'"'); //no change in output
    Code (markup):
     
    makamo66, Oct 2, 2013 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    Okay - you'll need to provide more of the code - and the actual output, and the code you use to output the variable - because something is wrong, somewhere, and it's not the str_replace-function. Unless cakephp does something with it / disables it, or something. I haven't used cakephp, so I can't say if they have some sort of internal cleanup function you should be using instead.
     
    PoPSiCLe, Oct 2, 2013 IP
  7. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #7
    simple str_replaces should have solved your problems. But if its not, try this:
    $comma_separated= "makamo66@hotmail.com','makamo66@gmail.com";
    $email_array = explode(",", $comma_separated);
    echo ($comma_separated);
    function trim_quotes(&$item1)
    {
        $item1 = trim($item1, '"');
        $item1 = trim($item1, "'");
    }
    array_walk($email_array,'trim_quotes');
    $clean_emails = implode(",", $email_array);
    PHP:
    I removed both single and double quotes to clean the string.
     
    samyak, Oct 2, 2013 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #8
    @samyak -- no need for the function or walk as TRIM can accept multiple characters.

    $mailCommaDelimited = "makamo66@hotmail.com','makamo66@gmail.com";
    $mailList = explode(',', $mailCommaDelimited);
    foreach ($mailList as &$mail) $mail = trim($mail, '\'"');
    $mailCleaned = implode(',', $mailList);
    echo $mailCleaned;
    Code (markup):
    The real question to me is where/what the devil is outputting such a screwed up list without leading/trailing single quotes in the first place?!? This seems like code to 'fix' something that should be resolved/fixed somewhere else!
     
    deathshadow, Oct 3, 2013 IP
  9. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #9
    I knew trim can accept multiple characters, just trying to be a bit fancy. lol :)
     
    samyak, Oct 3, 2013 IP