Jump to content

Recommended Posts

I wasn't sure if I was supposed to put this in the Regex section, or PHP help. Seeing as I >think< the Regex part is fine.

 

public function br2nl($string)
    {
        return preg_replace('#<br(| )\/>#', "\n", $string);
    }

 

I built this to change <br /> tags to new lines. Could anyone tell me what I'm doing wrong, as it doesn't convert them.

Link to comment
https://forums.phpfreaks.com/topic/264081-function-not-working-properly/
Share on other sites

well your regex could be a little more efficient, but other than that, I see nothing wrong with it, problem is elsewhere.

 

I'll continue looking. But may I ask how I could improve the regex? Thanks.

 

Thanks for the Regex. I'll have to look into what the ~ means.

 

Also, since I don't want to create another thread: I decided to ask this here. What do you believe is neater?

 

//get appropriate icon for the section
if($s_forum['type'] > 3)
      ($s_forum['type'] == 4) ? $icon = '' : $icon = '';
else
      $icon = $forum->getIcon($s_forum['icon']);

 

or

 

//get appropriate icon for the section
if($s_forum['type'] > 3)
{
       ($s_forum['type'] == 4) ? $icon = '' : $icon = ''; 
}
else
{
       $icon = $forum->getIcon($s_forum['icon']);  
}

I'd ditch the regex completely and go with:

 

public function br2nl($string) {
    return str_replace(array("<br />", "<br>"), "\n ", $string);
}

 

that doesn't account for <BR> <BR /> <br/> <BR            /> etc... lots of other ugly formats that browsers will render properly

I'd ditch the regex completely and go with:

 

public function br2nl($string) {
    return str_replace(array("<br />", "<br>"), "\n ", $string);
}

 

that doesn't account for <BR> <BR /> <br/> <BR            /> etc... lots of other ugly formats that browsers will render properly

 

Good point.

Also, since I don't want to create another thread: I decided to ask this here. What do you believe is neater?

 

//get appropriate icon for the section
if($s_forum['type'] > 3)
      ($s_forum['type'] == 4) ? $icon = '' : $icon = '';
else
      $icon = $forum->getIcon($s_forum['icon']);

 

or

 

//get appropriate icon for the section
if($s_forum['type'] > 3)
{
       ($s_forum['type'] == 4) ? $icon = '' : $icon = ''; 
}
else
{
       $icon = $forum->getIcon($s_forum['icon']);  
}

 

Firstly, whether or not you use the brackets is personal choice, though you certainly need them if you want to execute more than one line of code inside the condition.  but as a side note, your ternary is kinda funky.  The ternary would be cleaner like so:

 

$icon = ($s_forum['type'] == 4) ? '' :  '';

 

But on that note... you are also assigning an empty string to $icon whether whether true or false, so there's no point in having that at all...unless you typoed and meant to assign different values, you should just do

 

if ($s_forum['type'] == 4) $icon = '';

 

 

Thanks for the Regex. I'll have to look into what the ~ means.

 

it is the pattern delimiter.  You used # in yours.  You can use pretty much any non-alphanumeric char as the pattern delimiter, I personally like ~ because IMO it looks cleaner and more different than other chars, easier to see them, especially if you use modifiers (like the i modifier i added to make it case in-sensitive). 

Thanks for the quick tip about the Regex stuff. Very useful.

 

Also, since I don't want to create another thread: I decided to ask this here. What do you believe is neater?

 

//get appropriate icon for the section
if($s_forum['type'] > 3)
      ($s_forum['type'] == 4) ? $icon = '' : $icon = '';
else
      $icon = $forum->getIcon($s_forum['icon']);

 

or

 

//get appropriate icon for the section
if($s_forum['type'] > 3)
{
       ($s_forum['type'] == 4) ? $icon = '' : $icon = ''; 
}
else
{
       $icon = $forum->getIcon($s_forum['icon']);  
}

 

Firstly, whether or not you use the brackets is personal choice, though you certainly need them if you want to execute more than one line of code inside the condition.  but as a side note, your ternary is kinda funky.  The ternary would be cleaner like so:

 

$icon = ($s_forum['type'] == 4) ? '' :  '';

 

But on that note... you are also assigning an empty string to $icon whether whether true or false, so there's no point in having that at all...unless you typoed and meant to assign different values, you should just do

 

if ($s_forum['type'] == 4) $icon = '';

 

Yeah, I haven't put anything in for the strings yet.

 

But wouldn't that also be a matter of preference for the ternaries? I mean, in the end it gets the same thing done. I'm just curious about this. I don't want to start a bad habit.

But wouldn't that also be a matter of preference for the ternaries? I mean, in the end it gets the same thing done. I'm just curious about this. I don't want to start a bad habit.

 

In this case, without seeing what you are assigning, yes, it is a matter of preference, a matter of code readability, but you just need to really pay attention to order of operations when it comes to ternaries, throwing in multiple assignment operators like that...you can very easily make your code behave unexpectedly. 

I still can't seem to figure out why this code doesn't work. I tested it on a basic string: '<br/>test<br/>test<br/>', and it worked properly.

 

public function br2nl($string)
    {
        return str_replace('<br />', "\n", $string);
        //return preg_replace('#<br(| )\/>#', "\n", $string);
    }

 

Here is where it's used:

 

<?php echo $base->br2nl($thread->formatPost($details[6], $details[4])); ?>

 

Format post:

 

public function formatPost($content, $username)
    {
        //get the rank of the user
        $user = $this->database->processQuery("SELECT `acc_status` FROM `users` WHERE `username` = ? LIMIT 1", array($username), true);
        
        //remove HTML from all posts besides administrators
        if($user[0]['acc_status'] < 4) $content = htmlentities($content, ENT_NOQUOTES);
        
        //now let's do BBCode for mods and admins
        if($user[0]['acc_status'] > 2)
        {
            $bbcode = array('#\[b\](.+?)\[\/b\]#', '#\[i\](.+?)\[\/i\]#');
            $replace = array('<b>$1</b>', '<i>$1</i>');
            $content = preg_replace($bbcode, $replace, $content);
            
            if($user[0]['acc_status'] == 4)
            {

                    //convert QUOTE BBcode to actual HTML format
                    $content = preg_replace('/\[quote\=(.+?)](.+?)\[\/quote\]/s', '<div style="border:1px solid #957C07;margin: 14px 0 0">
                    <p style="background:#645305;margin:0;padding:2px;font-style:normal">
                            <strong>Original Content</strong> (Posted by: $1)
                    </p>
                    <div style="position:relative;float:right;overflow:hidden;height:33px;top:-28px;left:10px">
                            <span style="color:#957C07;font-family:Engravers MT,Felix Titling,Perpetua Titling MT,Times New Roman;font-style:normal;font-size:120px;line-height:81px">"</span>
                    </div>
                    <div style="font-style:italic;margin:8px 6px">$2
                    </div>
                    </div>', $content); 
            }
        }
        
        return stripslashes($content);
    }

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.