Jump to content

Archived

This topic is now archived and is closed to further replies.

roxii

Regex Help

Recommended Posts

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 [b]$x[/b] number of tabs to get proper HTML tabbing.

If [b]$x = 2;[/b]

It should turn:

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

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

into this:

[code]
        <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.
[/code]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--quoteo(post=377747:date=May 28 2006, 06:34 AM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ May 28 2006, 06:34 AM) [snapback]377747[/snapback][/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 [b]/>[/b] can be ignored for nesting.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.