SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regular expression in php

    Hello all,

    I have a string
    $str='abc/;bcd;mjn;'
    (escaping ; between abc,bcd)

    i am using preg_split() to split this string but i wanted to split this string in

    abcbcd
    mjn

    regular expression which i am using is preg_split("/;/",$str); it result in abc,bcd,mjn not as i wanted
    any clue what i am suppose to do here to get needed output.

    Thanks & Regarsd,

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    pr,

    IMHO, / does not escape the ; and preg_split is operating correctly. join() the the first two array members ([1] and [2]) together.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    pr,

    IMHO, / does not escape the ; and preg_split is operating correctly. join() the the first two array members ([1] and [2]) together.

    Regards,

    DK
    Dk,

    Thats the other way what i wanted is if the delimiter is present in your data then how i will make the regular expression is there any way that ";" in data can be recognised???

    Thanks & regards,
    Pr

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the '\' character isn't recognized as an escape character unless it is in a string that is created using double quotes.

    Use single quotes to embed the '\' before your ';'

    If possible, I would move the '\' in front of the delimiter then use

    PHP Code:
    $array explode('\;'$yourstring); 
    to parse out your data.

  5. #5
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jgstevenson View Post
    the '\' character isn't recognized as an escape character unless it is in a string that is created using double quotes.

    Use single quotes to embed the '\' before your ';'

    If possible, I would move the '\' in front of the delimiter then use

    PHP Code:
    $array explode('\;'$yourstring); 
    to parse out your data.
    Thanks dear but i think i am not able to deliver the correct problem to all members here what i was wondering what if the delimiter i am using for explode is in data ..explode will chop of the string of data also????which i think is not a gud .???????that was an example which i wanted to make it clear.

    i hope now i wud be quite clear and members will respond on this
    Regards

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, can't you just make your delimiter something that you are sure of won't appear in the data? Like ~@@Delimiter@@~ or something similiar.

  7. #7
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $str='abc/;bcd;mjn;';
    $array preg_split('#(?<!/);#'$str); 
    is that what you are wanting?

  8. #8
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jgstevenson View Post
    well, can't you just make your delimiter something that you are sure of won't appear in the data? Like ~@@Delimiter@@~ or something similiar.
    For now this is what i opted for :-) but does it mean that you cann't recognize that your delimiter is in data then you sud not split the text....????

  9. #9
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aamonkey View Post
    PHP Code:
    $str='abc/;bcd;mjn;';
    $array preg_split('#(?<!/);#'$str); 
    is that what you are wanting?
    hmm gr8 !!
    this seems to be gud escape from the problem.
    Can you please explain this to me how this has to be interpreted ?? spl "?<!"

    Thanks & Regards,
    Pr

  10. #10
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by priti View Post
    For now this is what i opted for :-) but does it mean that you cann't recognize that your delimiter is in data then you sud not split the text....????
    No matter what you choose as a delimiter you will have the problem. Any delimiter you choose if it's /; or ~@@Delimiter@@~ you can't have that be in the text that is going to be split a part. With the regular expression presented the the previous quotes, can you have a / at the end of your text that is going to be split out? Such as if you wanted to parse out phone/;address/;social/;

    into

    phone/
    address/
    social/

    it won't work. You'll run the regular expression and it will treat phone/;address/;social/; as one string. It only looks for the string that doesn't have a / next to a ;


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
  •