BillyBoB Posted June 2, 2008 Share Posted June 2, 2008 I am trying to format the selection so that it can't have any linebreaks before the starting of the text. The problem is that in the selection before the text there can other html codes. I just want to remove only the <br/> codes. Any ideas? Quote Link to comment Share on other sites More sharing options...
effigy Posted June 2, 2008 Share Posted June 2, 2008 preg_replace('%\A(?:<br/>)+%', '', $data); Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted June 2, 2008 Author Share Posted June 2, 2008 That didn't work. I want it to parse all <br/> before the first actually displayed text is shown. EX: < br/><html><head>< br/>a This should end up like <html><head>a Quote Link to comment Share on other sites More sharing options...
effigy Posted June 2, 2008 Share Posted June 2, 2008 <pre> <?php $data = '<br/><html><head><br/>a'; echo htmlspecialchars(preg_replace('%\A(?:<br/>)+%', '', $data)); ?> </pre> Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted June 2, 2008 Author Share Posted June 2, 2008 That doesn't work either. Its the same exact regex. I need the html to stay as html. I want just the <br/> to be deleted until the first actual output letter or number. So: <br/><html><head></head><body><br/><br/>a<br/></body></html> Changes to: <html><head></head><body>a<br/></body></html> Quote Link to comment Share on other sites More sharing options...
effigy Posted June 2, 2008 Share Posted June 2, 2008 Gotcha; the forum-botched HTML wasn't helping. htmlspecialchars is used to show the result. <pre> <?php $data = '<br/><html><head></head><body><br/><br/>a<br/></body></html>'; $pieces = preg_split('%(?<=>)(?!\s+<)([^<>]+)(?=<)%', $data, 2, PREG_SPLIT_DELIM_CAPTURE); $pieces[0] = str_replace('<br/>', '', $pieces[0]); $data = join('', $pieces); echo htmlspecialchars($data); ?> </pre> Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted June 2, 2008 Author Share Posted June 2, 2008 Worked perfectly. If you don't mind would you explain the regex to this? I would love to learn how to do it myself. Or simply point me in the direction of the best tutorials and books you have learned from. Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted June 3, 2008 Author Share Posted June 3, 2008 Bump could you explain the regex please? Quote Link to comment Share on other sites More sharing options...
effigy Posted June 3, 2008 Share Posted June 3, 2008 Actually, it could be reduced to: $pieces = preg_split('%(?<=>)(?!\s*<)%', $data, 2); (?<=...) is a positive lookbehind which is used to match a position rather than a character. Thus, (?<=>) means "Is there a '>' preceding this position?" If true, we know we're just beyond the ending of a tag. From here we look to see if content exists with (?!\s*<), which is a negative lookahead. That is, the match is successful if the position is not followed by 0 to ∞ (theoretically) instances of whitespace (\s*), followed by an opening tag. For further information see http://www.regular-expressions.info or continue questioning. Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted June 3, 2008 Author Share Posted June 3, 2008 It is deleting too much. This is what I have to remove the line breaks, in earlier code I formatted the line breaks \n into [ br]. <?php $pieces = preg_split('%(?<=>)(?!\s*<)%', $selection, 2, PREG_SPLIT_DELIM_CAPTURE); $pieces[0] = str_replace('[br]', '', $pieces[0]); return join('', $pieces); ?> From my observations this doesn't work at all. It just deletes all the line breaks in the whole selection. This is selecting the whole text. I want it only to select the info behind the first actually literal outside of the <> brackets. Quote Link to comment Share on other sites More sharing options...
effigy Posted June 3, 2008 Share Posted June 3, 2008 Are you referring to the original pattern, the new pattern, or both? What's the data set now? Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted June 3, 2008 Author Share Posted June 3, 2008 I'm referring to all of it. Also it isn't always <> brackets that is before the starting of the actual literals there could be [] brackets too because this is mostly for forums. Quote Link to comment Share on other sites More sharing options...
effigy Posted June 3, 2008 Share Posted June 3, 2008 Do you have a data sample that covers everything--all variations? This is getting confusing again. Why would the data intermix <> and []? Quote Link to comment Share on other sites More sharing options...
discomatt Posted June 3, 2008 Share Posted June 3, 2008 There's some approach being used here that is horribly wrong. Why are you having to jump through so many hoops to get the content the way you want? Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted June 3, 2008 Author Share Posted June 3, 2008 Alright the reasoning for the confusion is that the input data is going to be unexpected. Because the user is the one to place in the data. Example Data: [ code] <?php echo \$string; ?> [ /code] Without the spaces of course this is the data that gave me the unexpected outcome. Actually I take back there being any html tags all should be bbcode tags. The problem I have is if somebody use a lot of tags and spaces them out with line breaks I don't want them to appear if it is in he front of the message. Example: [ table] [ tr] [ td] info here [ b]Hi[ /b] [ /td] [ /tr] [ /table] This will display something like: info here Hi Quote Link to comment Share on other sites More sharing options...
effigy Posted June 4, 2008 Share Posted June 4, 2008 Is the desired result... [ table][ tr][ td]info here [ b]Hi[ /b] [ /td] [ /tr] [ /table] ...which strips all new lines before the content? Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted June 4, 2008 Author Share Posted June 4, 2008 Yep Quote Link to comment Share on other sites More sharing options...
effigy Posted June 4, 2008 Share Posted June 4, 2008 <pre> <?php echo $data = <<<DATA [table] [tr] [td] info here [b]Hi[/b] [/td] [/tr] [/table] DATA; echo '<hr>'; $pieces = preg_split('%(?<=\])(?>\s*)(?!\[)%', $data, 2); $pieces[0] = preg_replace('/(?<=\])\s+(?=\[)/', '', $pieces[0]); $data = join('', $pieces); echo $data; ?> </pre> Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted June 4, 2008 Author Share Posted June 4, 2008 The problem with using the positive lookbehind is what if they don't start with a bbcode tag? Example: Hi [ table] [ tr] [ td] info here Hi [ /td] [ /tr] [ /table] http://dreamshowstudios.net/development/BBCode/test.php Is where I tested it as you can see it messes up. Quote Link to comment Share on other sites More sharing options...
effigy Posted June 5, 2008 Share Posted June 5, 2008 %(?<=\]|\A)(?>\s*)(?!\[)% Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted June 5, 2008 Author Share Posted June 5, 2008 Perfect. Quote Link to comment 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.