Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: explode() and escaped characters?

  1. #1
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default explode() and escaped characters?

    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!
    My site: FreshCut :)

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    jlizarraga (05-03-2010)

  4. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,634
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by djr33 View Post
    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.

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.)
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,634
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

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

  7. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,429
    Thanks
    105
    Thanked 117 Times in 115 Posts

    Default

    PHP Code:
    <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.
    To choose the lesser of two evils is still to choose evil. My personal site

  8. The Following User Says Thank You to james438 For This Useful Post:

    jlizarraga (05-03-2010)

  9. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #8
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,429
    Thanks
    105
    Thanked 117 Times in 115 Posts

    Default

    Shoot, I hadn't thought of that.
    To choose the lesser of two evils is still to choose evil. My personal site

  11. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  12. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,634
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •