Jump to content

[SOLVED] Removing linebreak code from beginning of code.


BillyBoB

Recommended Posts

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>

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>

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.

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.

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
<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>

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.

Archived

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

×
×
  • Create New...

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.