roxii Posted May 28, 2006 Share Posted May 28, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10621-regex-help/ Share on other sites More sharing options...
poirot Posted May 28, 2006 Share Posted May 28, 2006 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 Link to comment https://forums.phpfreaks.com/topic/10621-regex-help/#findComment-39626 Share on other sites More sharing options...
roxii Posted May 28, 2006 Author Share Posted May 28, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/10621-regex-help/#findComment-39649 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.