PHP line break trim

  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

How can I do this code easier? It just seems too cluttery and I've been awake too long & haven't PHP'd it up in a while...

Code: [ Select ]
<?php
//This should remove line breaks from the beginning and ending of a string regardless of spacing.
$string = trim($string);
if(stripos($string,'<br />') === 0) { $string = substr($string,6); }
if(substr($string,-6) == '<br />') { $string = substr($string,0,-6); }
$string = trim($string);//remove any extra spaces incase there was some after line breaks that were removed.
?>
  1. <?php
  2. //This should remove line breaks from the beginning and ending of a string regardless of spacing.
  3. $string = trim($string);
  4. if(stripos($string,'<br />') === 0) { $string = substr($string,6); }
  5. if(substr($string,-6) == '<br />') { $string = substr($string,0,-6); }
  6. $string = trim($string);//remove any extra spaces incase there was some after line breaks that were removed.
  7. ?>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

What about
Code: [ Select ]
$str = preg_replace('#^(?:\s|<br[^>]*>)+|(?:\s|<br[^>]*>)+$#ism', '', $str);
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

Yeah that works pretty good, I should have explained myself better though.

I have a function that basically processes BBCode. I like doing it on my own because I made some of my own special types of BBCode for my website and I was working on processing list's using "preg_replace_callback()". This is what I have that works at the moment. I'm looking for some constructive criticism I guess, I'm sure half of my code could be done easier lol.

Code: [ Select ]
<?php
if(!function_exists('SubListReplace'))
{
    function SubListReplace($matches)
    {
    //Entire String is in $matches[0]
    $ListType = substr($matches[1],1);//could be empty or '=a','=1', etc...
    $SubList = $matches[2];
    $Items = explode('[*]',$SubList);
 
    $List = '';
     foreach($Items as $Item)
     { //This should remove line breaks from the beginning and ending of a string regardless of spacing.
     $Item = trim($Item);
      if(stripos($Item,'<br />') === 0) { $Item = substr($Item,6); }
      if(substr($Item,-6) == '<br />') { $Item = substr($Item,0,-6); }
     $Item = trim($Item); //remove any extra spaces incase there was some after line breaks that were removed.
 
//$Item = preg_replace('#^(?:\s|<br[^>]*>)+|(?:\s|<br[^>]*>)+$#ism', '', $Item);
      if(!empty($Item))
      {
      $List .= '<li>'.$Item.'</li>';
      }
     }
 
     switch($ListType)
     {
     case 'a':
     $List = '<ol style="list-style-type: upper-latin; ">'.$List.'</ol>';
     break;
     case 'la':
     $List = '<ol style="list-style-type: lower-latin; ">'.$List.'</ol>';
     break;
     case 'r':
     $List = '<ol style="list-style-type: upper-roman; ">'.$List.'</ol>';
     break;
     case 'lr':
     $List = '<ol style="list-style-type: lower-roman; ">'.$List.'</ol>';
     break;
     case '1':
     $List = '<ol>'.$List.'</ol>';
     break;
     default:
     $List = '<ul>'.$List.'</ul>';
     break;
     }
 
    return $List;
    }
}
 
// [ list][ *][ /list][ /list]
$Post_Text = preg_replace_callback("#\[list(=[0-9a-z]{0,2}){0,1}\](.*?)\[/list\]#si",'SubListReplace', $Post_Text);
?>
  1. <?php
  2. if(!function_exists('SubListReplace'))
  3. {
  4.     function SubListReplace($matches)
  5.     {
  6.     //Entire String is in $matches[0]
  7.     $ListType = substr($matches[1],1);//could be empty or '=a','=1', etc...
  8.     $SubList = $matches[2];
  9.     $Items = explode('[*]',$SubList);
  10.  
  11.     $List = '';
  12.      foreach($Items as $Item)
  13.      { //This should remove line breaks from the beginning and ending of a string regardless of spacing.
  14.      $Item = trim($Item);
  15.       if(stripos($Item,'<br />') === 0) { $Item = substr($Item,6); }
  16.       if(substr($Item,-6) == '<br />') { $Item = substr($Item,0,-6); }
  17.      $Item = trim($Item); //remove any extra spaces incase there was some after line breaks that were removed.
  18.  
  19. //$Item = preg_replace('#^(?:\s|<br[^>]*>)+|(?:\s|<br[^>]*>)+$#ism', '', $Item);
  20.       if(!empty($Item))
  21.       {
  22.       $List .= '<li>'.$Item.'</li>';
  23.       }
  24.      }
  25.  
  26.      switch($ListType)
  27.      {
  28.      case 'a':
  29.      $List = '<ol style="list-style-type: upper-latin; ">'.$List.'</ol>';
  30.      break;
  31.      case 'la':
  32.      $List = '<ol style="list-style-type: lower-latin; ">'.$List.'</ol>';
  33.      break;
  34.      case 'r':
  35.      $List = '<ol style="list-style-type: upper-roman; ">'.$List.'</ol>';
  36.      break;
  37.      case 'lr':
  38.      $List = '<ol style="list-style-type: lower-roman; ">'.$List.'</ol>';
  39.      break;
  40.      case '1':
  41.      $List = '<ol>'.$List.'</ol>';
  42.      break;
  43.      default:
  44.      $List = '<ul>'.$List.'</ul>';
  45.      break;
  46.      }
  47.  
  48.     return $List;
  49.     }
  50. }
  51.  
  52. // [ list][ *][ /list][ /list]
  53. $Post_Text = preg_replace_callback("#\[list(=[0-9a-z]{0,2}){0,1}\](.*?)\[/list\]#si",'SubListReplace', $Post_Text);
  54. ?>


I like your regular expression a lot, however it does not allow for extra <br /> tags within the <li>'s of each list.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

What about

Code: [ Select ]
<?php
if(!function_exists('SubListReplace'))
{
    function SubListReplace($matches)
    {
    //Entire String is in $matches[0]
    $ListType = substr($matches[1],1);//could be empty or '=a','=1', etc...

    $List = preg_replace('#\s*(?:<br[^>]*>)?\[\*\](?:<br[^>]*>)?\s*#', '</li><li>', $matches[2]);
    $List = preg_replace('#^</li>|<li>\s*</li>|<li>$#', '', $List);

    switch($ListType)
    {
    case 'a':
    $List = '<ol style="list-style-type: upper-latin; ">'.$List.'</ol>';
    break;
    case 'la':
    $List = '<ol style="list-style-type: lower-latin; ">'.$List.'</ol>';
    break;
    case 'r':
    $List = '<ol style="list-style-type: upper-roman; ">'.$List.'</ol>';
    break;
    case 'lr':
    $List = '<ol style="list-style-type: lower-roman; ">'.$List.'</ol>';
    break;
    case '1':
    $List = '<ol>'.$List.'</ol>';
    break;
    default:
    $List = '<ul>'.$List.'</ul>';
    break;
    }

    return $List;
    }
}

// [ list][ *][ /list][ /list]
$Post_Text = preg_replace_callback("#\[list(=[0-9a-z]{0,2}){0,1}\](.*?)\[/list\]#si",'SubListReplace', $Post_Text);
?>
  1. <?php
  2. if(!function_exists('SubListReplace'))
  3. {
  4.     function SubListReplace($matches)
  5.     {
  6.     //Entire String is in $matches[0]
  7.     $ListType = substr($matches[1],1);//could be empty or '=a','=1', etc...
  8.     $List = preg_replace('#\s*(?:<br[^>]*>)?\[\*\](?:<br[^>]*>)?\s*#', '</li><li>', $matches[2]);
  9.     $List = preg_replace('#^</li>|<li>\s*</li>|<li>$#', '', $List);
  10.     switch($ListType)
  11.     {
  12.     case 'a':
  13.     $List = '<ol style="list-style-type: upper-latin; ">'.$List.'</ol>';
  14.     break;
  15.     case 'la':
  16.     $List = '<ol style="list-style-type: lower-latin; ">'.$List.'</ol>';
  17.     break;
  18.     case 'r':
  19.     $List = '<ol style="list-style-type: upper-roman; ">'.$List.'</ol>';
  20.     break;
  21.     case 'lr':
  22.     $List = '<ol style="list-style-type: lower-roman; ">'.$List.'</ol>';
  23.     break;
  24.     case '1':
  25.     $List = '<ol>'.$List.'</ol>';
  26.     break;
  27.     default:
  28.     $List = '<ul>'.$List.'</ul>';
  29.     break;
  30.     }
  31.     return $List;
  32.     }
  33. }
  34. // [ list][ *][ /list][ /list]
  35. $Post_Text = preg_replace_callback("#\[list(=[0-9a-z]{0,2}){0,1}\](.*?)\[/list\]#si",'SubListReplace', $Post_Text);
  36. ?>
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

It almost works. There is one problem though, the string that is being inputted (between the lists) in general (without the line breaks) looks like...

[*]Apple[*]Orange[*]Banana

and I think your expression needs it to be...

[*]Apple[*]Orange[*]Banana[*]

So instead of getting:
<ul><li>Apple</li><li>Orange</li><li>Banana</li></ul>
It gives me:
<ul><li>Apple</li><li>Orange</li><li>Banana<br /></ul>

Another problem is my big chunk of crap code would work with...

[*]Apple[*]Orange[*]Banana[*]
[*]Apple[*]Orange[*]Banana
Apple[*]Orange[*]Banana[*]
Apple[*]Orange[*]Banana

And yours only works with:

[*]Apple[*]Orange[*]Banana[*]

I still like your 2 lines of PCRE you wrote a heck of a lot better than my apx. 15 lines of code. I was thinking to fix it by just appending a [*] or </li> to the end of the string automatically. But it still wouldn't allow for "lazy-style" lists: Apple[*]Orange[*]Banana (lists that are missing a star in the front). I was thinking adding a [*] to the front or end if they weren't there, but if there was a <br />[*] at the front or any sort of white space it wouldn't work.

I was also wondering, what exactly is this doing with the line breaks?

Code: [ Select ]
 
(?:<br[^>]*>)
 
  1.  
  2. (?:<br[^>]*>)
  3.  


My PCRE isn't as good as it should be :(.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Looks to me that it is capturing the result without the appearance of <br> or <br /> in it. But I'll let Joebert tell you that. The reason for my post here is the following two "cheat sheets" for PCRE.

Resources:
http://www.phpguru.org/downloads/PCRE%20Cheat%20Sheet/PHP%20PCRE%20Cheat%20Sheet.pdf
http://www.regextester.com/pregsyntax.html
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

I understand now. My cheetsheets that I do have on my wall referred to the (?:) subpattern type as "grouping", which I consider esoteric. That's why I was confused. I like the 2nd link you sent, I actually had it bookmarked but didn't go to it lol... That's great, I'm such a moron for not knowing that, I could have used that like 20 times, I was trying to figure out that.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

lol stuff happens... at least you don't the stupidest mistakes like I do and then take 2 hours trying to figure out what the h*ll went wrong. :lol:
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Where are those <br/> elements comming from ? :scratchhead:
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

joebert wrote:
Where are those <br/> elements comming from ? :scratchhead:


It's coming from an nl2br() function call that takes place before a ton of other code in my bbcode function. I think it was there so all these other functions could work properly. There are a ton of pcre functions with single-line flags in them after it, i was thinking about changing them all, but there is so much code that things could go wrong.... I don't remember if I put the nl2br() there, or if I got it from some original bb code script.

I'm not too worried about all this anymore though, I think I'm just going to keep what I wrote, it's not worth spending so much time on when I have something that works.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Good plan.

I'm thinking swapping \n for <br> is probably something best done last, or close to it in the future though. I get the feeling you agree. :D
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

joebert wrote:
Good plan.

I'm thinking swapping \n for <br> is probably something best done last, or close to it in the future though. I get the feeling you agree. :D


lol, yes "I Concur" :lol:

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 74 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.