I'm having quite a time with this particular problem:
I have users that enter tag words as form input, let's say for a photo or a topic of discussion. They are allowed to delimit tags with spaces and commas, and can use quotes to encapsulate multiple words.
An example:
tag1, tag2 tag3, "tag4 tag4, tag4" tag5, "tag6 tag6"
So, as we can see here anything is allowed, but the problem is that splitting on commas obviously destroys tag4 (the tag inside quotes but with a comma).
I've tried tons of regex, using preg_split, preg_match, and more but cannot figure out a solution. If this cannot be done, then it's also completely okay to tell the user they are not allowed to use commas inside of quoted strings via error message - which I tried doing using preg_match but my regex fails.
My regex for preg_match to alert the user of the problem:
/".*,.*"/
This works fine, but if you put any other quotes after the first quoted string (referring to "tag6 tag6") then it fails when it should not, since there are no commas inside tag6.
9 8657
I'm no regex guru by any means, but here is something to try in case you haven't thought of thise already. Change your example to this:
tag1, tag2 tag3, \"tag4 tag4, tag4\" tag5, \"tag6 tag6\"
And see if that works in your expression. If so just do a addslashes() function on the tag string before you pass it to your regex.
I'm having quite a time with this particular problem:
I have users that enter tag words as form input, let's say for a photo or a topic of discussion. They are allowed to delimit tags with spaces and commas, and can use quotes to encapsulate multiple words.
An example:
tag1, tag2 tag3, "tag4 tag4, tag4" tag5, "tag6 tag6"
So, as we can see here anything is allowed, but the problem is that splitting on commas obviously destroys tag4 (the tag inside quotes but with a comma).
I've tried tons of regex, using preg_split, preg_match, and more but cannot figure out a solution. If this cannot be done, then it's also completely okay to tell the user they are not allowed to use commas inside of quoted strings via error message - which I tried doing using preg_match but my regex fails.
My regex for preg_match to alert the user of the problem:
/".*,.*"/
This works fine, but if you put any other quotes after the first quoted string (referring to "tag6 tag6") then it fails when it should not, since there are no commas inside tag6.
That doesn't seem to make a difference in my particular case. Thanks for the reply, though! Any other thoughts?
I'm no regex guru by any means, but here is something to try in case you haven't thought of thise already. Change your example to this:
tag1, tag2 tag3, \"tag4 tag4, tag4\" tag5, \"tag6 tag6\"
And see if that works in your expression. If so just do a addslashes() function on the tag string before you pass it to your regex.
That doesn't seem to make a difference in my particular case. Thanks for the reply, though! Any other thoughts?
Well this is really dirty, but... what if you do a simple str_replace on quotes in the string (before regex) something like replacing the " with ~ or even ___QUOTE___, do your regex and then re-replace the ~ or ___QUOTE___ with " when you're done. Definitely not the best or fastest solution, but it should work.
Here's what I came up with: -
$str = 'tag1, tag2 tag3, "tag4 tag4, tag4" tag5, "tag6 tag6"';
-
print("$str<br /><br />\n\n");
-
-
$matches = preg_split('/(?<!\\\\)"/', $str);
-
-
$tags = array();
-
-
foreach($matches as $match) {
-
$innerTags = preg_split('/(?<!\\\\),/', $match);
-
foreach($innerTags as $tag)
-
if(preg_match('/\w/', $tag))
-
$tags[] = trim($tag);
-
}
-
-
var_dump($tags);
-
First, we split the string by '"', unless it's escaped (preceded by '\'). Now we know where the '"'s are.
Next, we run through each set of tags in between '"'s and split those by unescaped ',' (similarly to '\"', '\,' won't get split). Now we know where the individual tags are; all we have to do now is clean up.
We run through each potential tag and check to see if it has at least one alphanumeric character. If it does, we add it to $tags, sans leading/trailing whitespace.
This might not be the best way to do it; I whipped it up in about 10 minutes. But it works.
[EDIT: If you have magic_quotes turned on, you'll need to run stripslashes on your input first.]
I have finally found a way to do what is needed, based on what everyone has said so far. It is complicated, that much is true, but it works. The last solution posted did not work still, so I am forgetting Regex for now and simply using PHP's string functions. Test data: (string)$tag_list = tag1, tag2, "tag3 tag3, tag3" tag4, "tag5, tag5", tag6 tag7
[PHP]
// ensure there is at least one beginning and one ending quote
if(strpos($tag_list, '"') !== false && strrpos($tag_list, '"', 1) !== false) {
$quotes_found = true; // initialize the while loop
// find the position of the last quote
$last_quote_pos = strrpos($tag_list, '"', 1);
// initialize so that the first time we try and find the beginning quote we start from position 0
$end_quote_pos = 0;
// we go through the whole string until we reach the last quote
while($quotes_found) {
// find the position of the beginning quote for this tag string
$begin_quote_pos = strpos($tag_list, '"', ($end_quote_pos + 1));
// find the position of the end quote for this tag string
$end_quote_pos = strpos($tag_list, '"', ($begin_quote_pos + 1));
// have we reached the last quote?
if($begin_quote_pos == $last_quote_pos ||
$end_quote_pos == $last_quote_pos) {
$quotes_found = false; // set the while loop to stop after this
}
// split out the quoted tag string
$original_tag = substr($tag_list, $begin_quote_pos, ($end_quote_pos - $begin_quote_pos + 1));
// remove commas from the inside of the quoted string (this is the whole point of this entire if/while combo)
$fixed_tag = strtr($original_tag, ',', ' ');
// replace the quoted string tags with the fixed ones (sans commas)
$tag_list = strtr($tag_list, array($original_tag=>$fixed_tag));
} // end while going through the string with quotes
} // end making sure there is at least one beginning and end quote
[/PHP] End result: (string)$tag_list = tag1, tag2, "tag3 tag3 tag3" tag4, "tag5 tag5", tag6 tag7
I can now do what was originally sought out, which is to use explode() on $tag_list commas and/or spaces, giving me what I originally needed - which is a list of the tags in an array!
Heh. Oops. My code doesn't check to see if the comma is inside of quotes (which was the whole point).
Well at any rate, I'm glad you found a solution for your problem.
I couldn't deal with the fact that I couldn't figure this out, so I gave it one more go, and I think I've got it this time: -
<?php
-
$str = 'tag1, tag2 tag3, "tag4 tag4, tag4" tag5, "tag6 tag6"';
-
print("$str<br /><br />\n\n");
-
-
$matches = preg_split('/(?<!\\\\)"/', $str);
-
-
$tags = array();
-
-
foreach($matches as $idx => $match) {
-
if($idx % 2)
-
$tags[] = stripslashes(trim($match));
-
else {
-
$innerTags = preg_split('/(?<!\\\\),/', $match);
-
foreach($innerTags as $tag)
-
if(preg_match('/\w/', $tag))
-
$tags[] = stripslashes(trim($tag));
-
}
-
}
-
-
var_dump($tags);
-
?>
-
The only difference is that we directly copy odd-index matches instead of parsing them for commas.
Think about it. When you split by quote, matches 0, 2, 4, etc. are outside of the quotes, but matches 1, 3, 5 are inside. So matches 1, 3 and 5 should not be parsed, but matches 0, 2 and 4 should. If you don't believe me, var_dump $matches.
Also, I added stripslashes so that if you escape a quote or comma, it doesn't print the slash.
Here's what I get for output: -
tag1, tag2 tag3, "tag4 tag4, tag4" tag5, "tag6 tag6"
-
-
array(5) {
-
[0]=>
-
string(4) "tag1"
-
[1]=>
-
string(9) "tag2 tag3"
-
[2]=>
-
string(15) "tag4 tag4, tag4"
-
[3]=>
-
string(4) "tag5"
-
[4]=>
-
string(9) "tag6 tag6"
-
}
-
[EDIT: If you want to split tags by space in addition to comma, use this regex instead: - $innerTags = preg_split('/(?<!\\\\)((,\s*)|((?<!,)\s+))/', $match);
This splits by either comma (with optional following whitespace character[s]) or else just a whitespace character[s] (with a negative lookbehind so that we don't end up catching '\,' properly only to then split at the following whitespace character).
This will result in the following output: -
tag1, tag2 tag3, "tag4 tag4, tag4" tag5, "tag6 tag6"
-
-
array(6) {
-
[0]=>
-
string(4) "tag1"
-
[1]=>
-
string(4) "tag2"
-
[2]=>
-
string(4) "tag3"
-
[3]=>
-
string(15) "tag4 tag4, tag4"
-
[4]=>
-
string(4) "tag5"
-
[5]=>
-
string(9) "tag6 tag6"
-
}
-
Or try this on for size: -
tag1, tag2\ tag3, "tag4 tag4, tag4" tag5\, \"tag6 tag6\"
-
-
array(5) {
-
[0]=>
-
string(4) "tag1"
-
[1]=>
-
string(9) "tag2 tag3"
-
[2]=>
-
string(15) "tag4 tag4, tag4"
-
[3]=>
-
string(11) "tag5, "tag6"
-
[4]=>
-
string(5) "tag6""
-
}
-
]
[EDIT EDIT: The one big flaw in these regexes is that they don't check for escaped slashes (e.g., \\"). But you know what? My brain hurts. You go figure it out. o_O]
[EDIT EDIT EDIT: Nevermind. I got that working, too: -
<?php
-
$str = 'tag1, tag2\ tag3, "tag4 tag4, tag4" tag5\\\\, "tag6 tag6\"';
-
print("$str<br /><br />\n\n");
-
-
$matches = preg_split('/((?<!\\\\)|(?<=\\\\\\\\))"/', $str);
-
-
$tags = array();
-
-
foreach($matches as $idx => $match) {
-
if($idx % 2)
-
$tags[] = stripslashes(trim($match));
-
else {
-
$innerTags = preg_split('/((?<!\\\\)|(?<=\\\\\\\\))((,\s*)|((?<!,)\s))/', $match);
-
foreach($innerTags as $tag)
-
if(preg_match('/\w/', $tag))
-
$tags[] = stripslashes(trim($tag));
-
}
-
}
-
-
var_dump($tags);
-
?>
-
The major difference is that we will split if the quote or comma and/or/xor space is either not preceded by a slash or is preceded by two slashes.
Sample output (note that the comma after tag5 gets parsed properly): -
tag1, tag2\ tag3, "tag4 tag4, tag4" tag5\\, "tag6 tag6\"
-
-
array(5) {
-
[0]=>
-
string(4) "tag1"
-
[1]=>
-
string(9) "tag2 tag3"
-
[2]=>
-
string(15) "tag4 tag4, tag4"
-
[3]=>
-
string(5) "tag5\"
-
[4]=>
-
string(10) "tag6 tag6""
-
}
-
This is why pbmods should not be stuck in his apartment on a national holiday.]
This is why pbmods should not be stuck in his apartment on a national holiday.
Wow, that's a lot of work you did! That code is probably better than what I have and accounts for things that I hope users won't do but I'm sure they will.
Thanks!
Thanks!
You are welcome.
And don't worry about me; my templating engine needed an overhaul anyway ~_^
Sign in to post your reply or Sign up for a free account.
Similar topics
by: afrinspray |
last post by:
I'm writing a function that parses a nested list string that might look
like this:
( "HELLO WORLD!" 1231231 awesome ( 1 2 ) )
I wrote the logic already and it starts by splitting the string by...
|
by: Anders Dalvander |
last post by:
os.popen does not work with parameters inside quotes, nor do
os.popen. At least on Windows.
import os
cmd = '"c:\\command.exe" "parameter inside quotes"'
os.popen4(cmd)
Results in the...
|
by: beliavsky |
last post by:
The code
for text in open("file.txt","r"):
print text.replace("foo","bar")
replaces 'foo' with 'bar' in a file, but how do I avoid changing text
inside single or double quotes? For making...
|
by: GIMME |
last post by:
I can't figure an expression needed to parse a string.
This problem arrises from parsing Excel csv files ...
The expression must parse a string based upon comma delimiters,
but if a comma...
|
by: John Perks and Sarah Mount |
last post by:
I have to split some identifiers that are casedLikeThis into their
component words. In this instance I can safely use to represent
uppercase, but what pattern should I use if I wanted it to work...
|
by: Søren M. Olesen |
last post by:
Hi
When using RegEx to replace a text within a string, how do I prevent it from
replacing the text if it's inside a tag...For example if I want to change
'small' to 'large' in the following...
|
by: VMI |
last post by:
How can I split a string that looks like this:
John, Doe, 37282, box 2, 10001, "My description, very important", X, Home
If I use String.Split(), it'll split the string that's between the...
|
by: Michael Yanowitz |
last post by:
Hello:
If I have a long string (such as a Python file).
I search for a sub-string in that string and find it.
Is there a way to determine if that found sub-string is
inside single-quotes or...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |