3raser Posted June 13, 2012 Share Posted June 13, 2012 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 More sharing options...
.josh Posted June 13, 2012 Share Posted June 13, 2012 well your regex could be a little more efficient, but other than that, I see nothing wrong with it, problem is elsewhere. Link to comment https://forums.phpfreaks.com/topic/264081-function-not-working-properly/#findComment-1353353 Share on other sites More sharing options...
.josh Posted June 13, 2012 Share Posted June 13, 2012 p.s. i would write the regex like this: return preg_replace('~<br\s*/?>~i', "\n", $string); Link to comment https://forums.phpfreaks.com/topic/264081-function-not-working-properly/#findComment-1353354 Share on other sites More sharing options...
3raser Posted June 13, 2012 Author Share Posted June 13, 2012 Quote 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']); } Link to comment https://forums.phpfreaks.com/topic/264081-function-not-working-properly/#findComment-1353355 Share on other sites More sharing options...
KevinM1 Posted June 13, 2012 Share Posted June 13, 2012 I'd ditch the regex completely and go with: public function br2nl($string) { return str_replace(array("<br />", "<br>"), "\n ", $string); } Link to comment https://forums.phpfreaks.com/topic/264081-function-not-working-properly/#findComment-1353356 Share on other sites More sharing options...
.josh Posted June 13, 2012 Share Posted June 13, 2012 Quote 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 Link to comment https://forums.phpfreaks.com/topic/264081-function-not-working-properly/#findComment-1353357 Share on other sites More sharing options...
KevinM1 Posted June 13, 2012 Share Posted June 13, 2012 Quote Quote 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. Link to comment https://forums.phpfreaks.com/topic/264081-function-not-working-properly/#findComment-1353359 Share on other sites More sharing options...
.josh Posted June 13, 2012 Share Posted June 13, 2012 Quote 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 = ''; Link to comment https://forums.phpfreaks.com/topic/264081-function-not-working-properly/#findComment-1353360 Share on other sites More sharing options...
.josh Posted June 13, 2012 Share Posted June 13, 2012 Quote 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). Link to comment https://forums.phpfreaks.com/topic/264081-function-not-working-properly/#findComment-1353361 Share on other sites More sharing options...
3raser Posted June 13, 2012 Author Share Posted June 13, 2012 Thanks for the quick tip about the Regex stuff. Very useful. Quote Quote 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. Link to comment https://forums.phpfreaks.com/topic/264081-function-not-working-properly/#findComment-1353362 Share on other sites More sharing options...
.josh Posted June 13, 2012 Share Posted June 13, 2012 Quote 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. Link to comment https://forums.phpfreaks.com/topic/264081-function-not-working-properly/#findComment-1353364 Share on other sites More sharing options...
3raser Posted June 13, 2012 Author Share Posted June 13, 2012 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); } Link to comment https://forums.phpfreaks.com/topic/264081-function-not-working-properly/#findComment-1353401 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.