...

View Full Version : Delete string after comma



dreamingdigital
08-07-2007, 09:04 PM
How would I truncate a string after a certain character? I don't know the index of that character.

I think this is close, but I need to replace the 18 with the index of the comma in the variable mystring since I won't know where the user put in a comma.

$mystring = "Hello I'm a string, and I have a comma.";
$mystring = substr_replace($mystring , '', 18, -1);

my result of echoling $mystring would be:
"Hello I'm a string"

mlseim
08-07-2007, 09:08 PM
$mystring = "Hello I'm a string, and I have a comma.";
list ($before,$after) = split (',', $mystring);
echo $before;

I'm not sure if you have to escape the comma
in the split command or not ( '\,' )

dreamingdigital
08-07-2007, 09:44 PM
My server didn't require escaping the comma. Thanks! My brain works in Javascript and PHP doesn't always translate. :)



$has_comma = (stristr($your_email, ",")>-1) ? 1 : 0;
if ($has_comma) {
list ($before,$after) = split (',', $your_email);
$your_email = $before;
}

marek_mar
08-08-2007, 12:05 AM
if (($pos = strpos($str, ',')) !== false)
{
$str = substr($str, 0, $pos);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum