SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Text block regular expressions

    Everyones favourite subject is back!

    I have this regular expressions which is for text boxes in an article system so the user cvan align text (it will probably become [align] tags instead of text but for now that is irrelevant!

    $text = preg_replace("/\[text=([^\]]+)\]([^\]]+)\[\/text\]/ie","fwtext('$1','$2')",$text);

    Which works with the [text=right]text here[/text] I need the text and the alignment property out of it.

    Basically it only works if there are no other tags in it i.e. [img=1] because of I believe ([^\]]+) that bit where it says from what I have gathered from other topics here get everything upto the next ] how do I say get everything between the text and text tags? I want all characters except obviously the end tag [/text].

    So what I am trying to allow is [text]dfaghgfh[img=1]estr{quote}gdfhgfd{/quote}dfghgfhgf[/text]
    Therefore taking ALL the content from between the text tags, and it will format the rest further down the function! (Quote was adapted to stop the PHPBB codes).

    I realise that if some idiot embeds a text within a text it will grab the first end text tag but this will be a project further down the align and in this case it would be bad formating anyway!

    Thanks for your help.
    Last edited by SiCo; Mar 14, 2003 at 05:51.

  2. #2
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I lack the regex expertise to answer your question completely, but I can set you on the right track. The behavior you are looking for is called "non-greedy"; this should fuel a Google search for you .

    Sorry I couldn't be more help.
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks however I am not sure it is the greedy / un-greedy property is it? I don't see why it would be because it is still stopping at all occurances of ] which is not part of the end text tag so it isn't converting anything.

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you had the pattern better with .+? in the other thread.

    but the . metacharacter in preg doesn't match newlines by default. you need to use the s modifier:

    $text = preg_replace('#\[text=(.+?)](.+?)\[/text]#eis', "fwtext('$1','$2')", $text);

    hope that helps.
    Last edited by DR_LaRRY_PEpPeR; Mar 15, 2003 at 21:17.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks that works, I wonder if you can tell me the difference between:
    (.*?)
    (.+?)

    I have been fiddling, mainly messing things up with taking apart some other regular expressions to see if I could get what I wanted and these two appear to do the same?! I thought the * and + were different.

    Sorry abotut he two threads one started off with something else, and on here before someone requested a thread per different question, running forums myself I know how annoying it is

  6. #6
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    * = 0 or more.
    + = 1 or more
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Arh now it makes all the sense in the world cheers.


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
  •