Jump to content


Photo

Regex Help


  • Please log in to reply
2 replies to this topic

#1 roxii

roxii
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationNorway

Posted 28 May 2006 - 02:18 AM

I'm not very able with regular expressions, but I have learned a lot in the past few hours trying to get a problem solved. Alas, I am finally stuck. Here's what I'm trying to accomplish:

I have several blocks of text separated by at least one line break ("\r\n"). These blocks of text may have zero or more tabs ("\t") in front of them. I am trying to use preg_replace to find all the line breaks and replace them with a minium of $x number of tabs to get proper HTML tabbing.

If $x = 2;

It should turn:

<p>A paragraph.</p>
<ul>
    <li>Element 1</li>
    <li>Element 2</li>
</ul>

                        <p>Another paragraph.</p>
                A freestanding block of text.

into this:

        <p>A paragraph.</p>
        <ul>
            <li>Element 1</li>
            <li>Element 2</li>
        </ul>
<!-- No tabs here because it's an empty line -->
        <p>Another paragraph.</p>
        A freestanding block of text.

I'm don't fully understand the power of regular expressions, so I'm not sure if something like this is possible. I've been using [a href=\"http://www.regular-expressions.info/characters.html\" target=\"_blank\"]this site[/a] as a resource. If there is a better one out there, I'd like to know about it. This one is faily easy to comprehend even though its organization isn't quite suited for beginners.

I'll really appreciate help anyone has to offer.

#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 28 May 2006 - 03:34 AM

An easier solution, maybe... Try to remove ALL the tabs and whitespaces (something like an extended ltrim()) and THEN add tabs.

Obviously everything would be "tabbed" automatically and by the same number of tabs, but creating some script that can detect and understand nested tags is not something I am willing, and maybe capable of doing for now.
~ D Kuang

#3 roxii

roxii
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationNorway

Posted 28 May 2006 - 07:05 AM

[!--quoteo(post=377747:date=May 28 2006, 06:34 AM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ May 28 2006, 06:34 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
An easier solution, maybe... Try to remove ALL the tabs and whitespaces (something like an extended ltrim()) and THEN add tabs.

Obviously everything would be "tabbed" automatically and by the same number of tabs, but creating some script that can detect and understand nested tags is not something I am willing, and maybe capable of doing for now.
[/quote]

Yes, that is what I am using now, but I'm looking for the complex version. I should've noted in my post: only XHTML strict will be used for the tabbing, so any HTML tags ending in /> can be ignored for nesting.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users