Forum Moderators: coopster

Message Too Old, No Replies

preg_split

Splitting a string into an array

         

Paul in South Africa

1:39 pm on Oct 21, 2003 (gmt 0)

10+ Year Member



I am trying to build a script that will search a database based on the keywords that are inputted by the user. I have managed to split the string into an array using the following:

$keywords = preg_split("/[,¦OR]/", $keywords);

What I want it to do is split the string on , ¦ or OR. It does that but it will also split on O or R. Can someone point me in the right direction?

I also need to find the element(s) of that array that start and end with a quote. Each element of the array can consist of any combination of letters (and numbers) and spaces. I assume I need to use preg_match("/^\"[A-z0-9]*\"$/", $keywords[$i]) or something similar where $i is a number but I can't seem to get it to match.

coopster

2:11 pm on Oct 21, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I can tell you that it is splitting on the O and R because you have them inside of a character class (the brackets []). Try this:

$keywords = preg_split("/(,¦OR¦\¦)/", $keywords);

Note:
-----
This is case-sensitive! It won't split on 'or'. It will also split if there is a word that is capitalized, such as DOORWAY -- it would split and the array would contain two entries: 'DO' and 'WAY'.

I also need to find the element(s) of that array that start and end with a quote.

Loop through the elements:

foreach ($keywords as $value) if (preg_match("/^\".*\"$/", $value)) print $value;

Paul in South Africa

2:25 pm on Oct 21, 2003 (gmt 0)

10+ Year Member



Thanks Coopster. I did eventually manage to work that part out although your code is far more elegant than mine was. I solved the problem of the DOORWAY splitting into DO and WAY by making it split only if there is whitespace before and after the OR.

Now to try and work out how to determine if an element of the array starts and ends with double quotes. One day I will try to learn regular expressions properly.

coopster

2:32 pm on Oct 21, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You're welcome. The updated expression to find whitespace before and after 'OR':

$keywords = preg_split("/(,¦\s+OR\s+¦\¦)/", $keywords);

...and I edited my original post, realized I didn't answer part two of your question -- how to determine if an element of the array starts and ends with double quotes. See the updated post.

One day I will try to learn regular expressions properly.

Hey, so will I ;)

Xuefer

3:37 pm on Oct 21, 2003 (gmt 0)

10+ Year Member



"/([,¦]\\wOR\\w¦)/"
is this ok?

dcrombie

4:12 pm on Oct 23, 2003 (gmt 0)



I think what the original poster wanted to acheive, something I'd like to be able to do as well, was to split an input string of:

A B "C D" E

into four elements: "A"; "B"; "C D"; "E"

coopster

7:28 pm on Oct 27, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Actually I think your request is different, but you can get your parsed string into an array as well using preg_match_all:

$keywords = 'A B "C D" E';
preg_match_all("/\".*\"¦\b\S+\b/U", $keywords, $matches);
print "<pre>"; print_r($matches); exit("</pre>");

Note: There may be a better way to accomplish the goal desired, but this does work.