Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 10 of 10

This is a discussion on Stripping slashes from a string in the Shared & Semi-Dedicated forum
Well, of course you have the stripslahes() function ni PHP... But, i need a function that strips the slashes of a whole string except for ...

  1. #1
    Around to haunt and taunt
    Join Date
    Feb 2002
    Location
    Netherlands
    Posts
    14

    Stripping slashes from a string

    Well, of course you have the stripslahes() function ni PHP...

    But, i need a function that strips the slashes of a whole string except for the part that is between <ascii> </ascii>.

    So if i have this string:

    $str = "Hello, I\'m a \string <ascii>And I\'M looking good</ascii> Bal bla"

    And you do the function i want to have this string

    $str = "Hello, I'm a string <ascii>And I\'M looking good</ascii> Bal bla"

    I tried to look into regular expressions, but they aren't that easy to understand for a hobbycoder...

    any help will be appreciated...

    TBA
    sjonger:::

  2. #2
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    Its definitely a job for a regular expression, but kind of a tricky one. I'll give it some thought...

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  3. #3
    Jag Veteran
    Join Date
    Sep 2002
    Posts
    650
    PHP Code:

    $string 
    "Hello, I\'m a \string <ascii>And I\'M looking good</ascii> Bal bla <ascii>And I\'M looking good 2</ascii> bl
    a bla bla 2"
    ;
    $magic_string "/(.*?)(<ascii>.*?<\/ascii>)/e";
    print 
    preg_replace($magic_string"str_replace(\"\\'\",\"'\",'\\1').'\\2'"$string) . "\n"
    Have fun

    EDITED:
    This forum seems to escape half of the characters in the code and I even can't attach gif.


    $magic_string = "/(.*?)(<ascii>.*?<&#92;/ascii>)/e";
    print preg_replace($magic_string, "str_replace(&#92;"&#92;&#92;'&#92;",&#9 2;"'&#92;",'&#92;&#92;1').'&#92;&#92;2'" , $string) . "\n";
    Last edited by gerilya; 09-24-2003 at 07:34 PM.

  4. #4
    Jag Veteran
    Join Date
    Sep 2002
    Posts
    650
    Originally posted by jason
    Its definitely a job for a regular expression, but kind of a tricky one. I'll give it some thought...

    --Jason
    Nah, everyone who once took a C class can do it with strstr() function and a while loop. In fact, it's much easier and makes a code more readable, I just like to do things a hard way

  5. #5
    Kubla Khan lookout's Avatar
    Join Date
    Aug 2002
    Location
    Orodruin
    Posts
    1,386
    The gif was probably too big to post. 100k or so is probably the upper limit here; might be less.

    I've always thought doing things the hard way helps builds character. Though in this case, if done right, it should strip characters.

    That and a greater appreciation for elegant code....
    The trouble with our times is that the future is not what it used to be.
    - Paul Valery

  6. #6
    Jag Veteran
    Join Date
    Sep 2002
    Posts
    650
    Originally posted by lookout
    The gif was probably too big to post. 100k or so is probably the upper limit here; might be less.
    Try to attach a small gif into this thread and tell me how it is going


    I've always thought doing things the hard way helps builds character. Though in this case, if done right, it should strip characters.

    That and a greater appreciation for elegant code....
    LOL! This code is hardly elegant, not to mention complexity. And on top of that here is a bug: it doesn't strip slashes after the last </ascii>.

    Here is the fix that strips slahes ONLY after the last </ascii> :
    $magic_string = "/(.*<&#92;/ascii&#62;&#41;*(.*)/e";
    print preg_replace($magic_string, "'&#92;&#92;1' . str_replace(&#92;"&#92;&#92;'&#92;",&#92 ;"'&#92;",'&#92;&#92;2')", $string) . "\n";
    Last edited by gerilya; 09-25-2003 at 12:40 AM.

  7. #7
    Kubla Khan lookout's Avatar
    Join Date
    Aug 2002
    Location
    Orodruin
    Posts
    1,386
    You misunderstood what I was saying. I didn't mean your solution was elegant here or correct. I wasn't even analyzing it, other than thinking, that looks like a rather cumbersome way to accomplish the task at hand. All I was saying was that doing things the hard way often leads to a greater appreciation for simple, elegant solutions that one doesn't get otherwise.

    Let's see if this gif works now...
    The trouble with our times is that the future is not what it used to be.
    - Paul Valery

  8. #8
    Kubla Khan lookout's Avatar
    Join Date
    Aug 2002
    Location
    Orodruin
    Posts
    1,386
    Apparently not.

    Let's try a smaller one.
    The trouble with our times is that the future is not what it used to be.
    - Paul Valery

  9. #9
    Kubla Khan lookout's Avatar
    Join Date
    Aug 2002
    Location
    Orodruin
    Posts
    1,386
    No go there either. Well, they used to work here. What about a jpg?
    The trouble with our times is that the future is not what it used to be.
    - Paul Valery

  10. #10
    Kubla Khan lookout's Avatar
    Join Date
    Aug 2002
    Location
    Orodruin
    Posts
    1,386
    No go either. Seems like attachments have been disabled. Looks like one must link the image from a site on the net rather than attach it, like so?



    [appended]
    No? Seems like the feature has stopped working then.

    [appended again]
    Ok, I've started another thread on the image attachment thing, so the rest of you can get back to slash slashing methods if you like.
    Last edited by lookout; 09-25-2003 at 08:12 AM.
    The trouble with our times is that the future is not what it used to be.
    - Paul Valery

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
  •