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);
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
I wasn't specific enough. It's the double quotation marks that I'm trying to remove. Not the single ones.
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
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):
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.
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 -- 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!