Jump to content

[SOLVED] Removing linebreak code from beginning of code.


BillyBoB

Recommended Posts

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.