View Full Version : explode() and escaped characters?
jlizarraga
04-30-2010, 07:37 PM
Hi all,
So I have some URLs that look like this:
/details/Type+Year+Make+Model+Trim+VIN
I then explode() everything after "/details/" using "+" as the delimiter.
Problem is, today I'm encountering some data where the "Trim" part has a "+" in it. Here's the string in question:
New+2010+Kia+Soul+2.0L I4 ++KNDJT2A2XA7105256
To try and solve this, I escaped the + like so:
New+2010+Kia+Soul+2.0L I4 %2B+KNDJT2A2XA7105256
When I explode() this string, PHP recognizes the "%2B" as a "+" and treats it as a delimiter.
How can I escape the "+" so that explode() won't treat it as a delimiter?
Thanks!
djr33
04-30-2010, 07:50 PM
I'm not sure if you can. You could try to double-encode the string-- do urlencode() before you submit it as the url then have it do it again (or do it manually both times)-- use trial and error to figure out how to best do that. THEN add the real + signs. But the basic problem here is that you are using a character for "grammatical" purposes and also "information" purposes. You just need to find a way to not do that. For example, in that trim, rename it "plus" or whatever you'd like.
But there's no trick to the programming here-- it's just a problem with the logic of using one thing for two different reasons without any way to distinguish them.
The one final possibility here is that if you are POSITIVE the + will only appear within the trim section and NEVER one of the others, you can do this:
Use explode with + for the number of parts minus one (leaving trim and VIN together as one piece).
Then take the last part of that (trim+VIN) and split it at the LAST + sign and only once.
This is of course a little more complex than just using explode(), but it can work.
To do this:
1. For the first explode(), add the 3rd parameter of a limit for the 5 parts (not 6).
2. Use a mix of strpos() and substr() to split that last element at the last + in it.
the basic problem here is that you are using a character for "grammatical" purposes and also "information" purposes. You just need to find a way to not do that. For example, in that trim, rename it "plus" or whatever you'd like.
Unless you could use a different delimiter altogether, I'd go that route - write the info as 2.0L I4 PLUS. If you need it to be a + sign, just use str_replace('PLUS', '+', $explodedstring) afterwards.
djr33
05-01-2010, 01:57 AM
Yes, that's the better answer. It's actually possible to do that both before and after sending it through the URL so that the only place it shows up like that is in the URL itself-- however, that will of course make the URL look a little funny if that matters to you...
(And in theory you might get some rare false positives and replace something you don't want to with +, like "SURPLUS"="SUR+", etc.)
in that case, use BBCode-style tags:
str_replace=('[PLUS/]', '+', $exploded string)
Unlikely to be replaced unintentionally. But also tedious to work with. We might be getting carried away here...
james438
05-03-2010, 04:22 AM
<pre><?php
$summary="New+2010+Kia+Soul+2.0LI4++KNDJT2A2XA7105256";
$summary=preg_split('/\+(?=\w)/',$summary);
print_r($summary);
?></pre>
The above will split based on a regular expression where '+' is recognized as a delimiter only if it is followed by a letter or a number or the underscore.
djr33
05-03-2010, 06:00 AM
That's not necessarily going to work, though: that only works if the + is always at the end. What is the trim is called "+1 trim" or something?
Of course if it's always exactly that way, it will work.
james438
05-03-2010, 06:19 AM
Shoot, I hadn't thought of that.
djr33
05-03-2010, 06:22 AM
Yeah, the problem here is very complex. The only real possibility without effective escaping is to do what I said earlier and ONLY if it's going to only be the "trim" that might have a plus in it. If it might be other categories, it's very messy.
Of course if there is only this single exception (one type of trim with one +), then any method including yours would be just fine.
Basically, we need an escape character here. Like you can backslash special characters in PHP, SQL, etc. to treat them like literal characters (e.g., " \' " shows " ' ", " \\' " shows " \ ", and so forth). But that's a complicated regex, and I don't think it's necessary in this case.
jlizarraga
05-03-2010, 10:54 PM
Thanks for all the informative responses!
We ended up using the "PLUS" method. This is the only time a "+" has ever appeared in our data, ever (and we are talking about mountains of data here). If any more +'s ever pop up in different fields, we've got an army of solutions in this topic. :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.