Closed Thread Icon

Topic awaiting preservation: Spliting strings in PHP Pages that link to <a href="https://ozoneasylum.com/backlink?for=29660" title="Pages that link to Topic awaiting preservation: Spliting strings in PHP" rel="nofollow" >Topic awaiting preservation: Spliting strings in PHP\

 
Author Thread
hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana
Insane since: Aug 2000

posted 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));


This works fine unless the input contains quote '"', less-than '<', greater-than '>', or ampersand '&'. It seems that preg_replae, preg_split, ereg_replace, and split substitue the entity equivalent for these four characters. This causes the input to be split at the wrong place.

Is there a way for me to use a regular expression to split the input into fields without having it substitue entities?

.



-- not necessarily stoned... just beautiful.


(Edited by hyperbole on 10-30-2007 04:32)

zavaboy
Paranoid (IV) Inmate

From: f(x)
Insane since: Jun 2004

posted posted 10-30-2007 04:51

I think this should work for you.

code:
$fields = explode(';', str_replace(array("\n","\r"), '', $input));


EDIT: By the sound if it, your text contains HTML entities (eg: "&lt;" for "<"), if this is the case, use html_entity_decode() on the string before doing the above.



(Edited by zavaboy on 10-30-2007 04:58)

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana
Insane since: Aug 2000

posted 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.

The string doesn't contain entities before I send it to preg_replace, but it does in the resulting string.

For example, with an input string of 'A"Z;10', I tried

code:
$temp = preg_replace('/"/', '!quot!', $input);
 $temp = preg_replace('/&/', '!amp!', $temp);
 echo "$temp<br/>";



This prints 'A!amp!quot!;Z;10'

.



-- not necessarily stoned... just beautiful.

zavaboy
Paranoid (IV) Inmate

From: f(x)
Insane since: Jun 2004

posted 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)))));


I know it looks like a lot, but it clocks a little better than what you had according to my benchmarks.

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 10-30-2007 10:41

just use $array = explode(';'...)
followed by php->map('strip' $array) (drop that php prefix...)

That's a cleaner and more elegant way to show what you're doing.

zavaboy
Paranoid (IV) Inmate

From: f(x)
Insane since: Jun 2004

posted posted 10-30-2007 20:12

I think TP means: array_map('trim', $array) (link: php->array_map )

You will still have to use html_entity_decode() in order to keep the HTML entities intact from the explode(). You will also still have to use the str_replace() to remove any newlines if that is what you want. If you need to protect the output, you will have to use another array_map() with htmlspecialchars to add HTML entities back into it.

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana
Insane since: Aug 2000

posted posted 10-30-2007 21:37

zavaboy,

You are right, the input string does contain entities. I had been echoing the string and the entities were showing in the browser as, for example, a quote("). But when I looked at the page source, they were actually entities. So, I took your advice and used html_entitiy_decode().

Tyberious Prime, I couldn't find anything about php->map() in the php documentation and when I tried to use it, I got an error that the function wasn't recognized.

Thanks for your help with this, both of you

.



-- not necessarily stoned... just beautiful.

« BackwardsOnwards »

Show Forum Drop Down Menu