Topic awaiting preservation: Spliting strings in PHP |
|
---|---|
Author | Thread |
Paranoid (IV) Inmate From: Madison, Indiana |
posted 10-30-2007 04:31
I'm fairly new to PHP and am having a problem processing some input strings. I'm taking input from a textarea that will have two fields separated by a semi-color ';'. I'm using the following line to separate the line inot fields: code: $fields = preg_split('/ *; */', preg_replace("/[\n\r]/", "", $input));
|
Paranoid (IV) Inmate From: f(x) |
posted 10-30-2007 04:51
I think this should work for you. code: $fields = explode(';', str_replace(array("\n","\r"), '', $input));
|
Paranoid (IV) Inmate From: Madison, Indiana |
posted 10-30-2007 07:39
The problem with explode is that is doesn't accept a regular expression. I want to use the regular expression / *; */ to remove any spaces around the semi-colon at the same time I split the string. code: $temp = preg_replace('/"/', '!quot!', $input); $temp = preg_replace('/&/', '!amp!', $temp); echo "$temp<br/>";
|
Paranoid (IV) Inmate From: f(x) |
posted 10-30-2007 09:17
That means the string has HTML entities, where from I can't say. So this should do what you want: code: $fields = explode(' ; ', htmlspecialchars(preg_replace('/\s*;\s*/',' ; ',str_replace(array("\n","\r"), '', html_entity_decode($input)))));
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 10-30-2007 10:41
just use $array = explode(';'...) |
Paranoid (IV) Inmate From: f(x) |
posted 10-30-2007 20:12
I think TP means: array_map('trim', $array) (link: php->array_map ) |
Paranoid (IV) Inmate From: Madison, Indiana |
posted 10-30-2007 21:37
zavaboy, |