Mencarta Posted March 1, 2010 Share Posted March 1, 2010 I have a script that finds and and then replaces that with <b> and </b> so it when I echo it, it is in bold. I get the error: Parse error: syntax error, unexpected '{' in C:\apache\server\php\fml.php on line 35. Thanks.fml.php <html> <head> <title>Server - PHP - FML</title> <style> a, a:link, a:visited, a:hover { color:blue; } ul { list-style-type:none; position:relative; left:-17px; } </style> </head> <body> <center> <h1>Server - PHP - FML</h1> <p>Please enter some text that has the text "[b]" and "[/b]".</p> <br /> <?php if (!isset($_POST["text"])) { ?> <form action="/" method="post"> <input type="text" name="text" /> <br /> <input type="submit" value="Submit" /> </form> <?php } if (isset($_POST["text"])) { if (strstr($_POST["text"],"[b]") { if (strstr($_POST["text"],"[/b]") { $text = str_replace("[b]","<b>",$text); $text = str_replace("[/b]","</b>",$text); } else { $_POST["text"] += "[/b]"; $text = str_replace("[b]","<b>",$text); $text = str_replace("[/b]","</b>",$text); } } else { echo $_POST["text"] } } ?> </center> </body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted March 1, 2010 Share Posted March 1, 2010 The forum is parsing your bold tags, but it looks like line 35 is missing a closing parenthesis. Link to comment Share on other sites More sharing options...
Mencarta Posted March 1, 2010 Author Share Posted March 1, 2010 lol, Thanks. Link to comment Share on other sites More sharing options...
Mencarta Posted March 1, 2010 Author Share Posted March 1, 2010 I fixed all those errors, but its just blank now. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 2, 2010 Share Posted March 2, 2010 You're not seeing the <h1> or other elements? Link to comment Share on other sites More sharing options...
Mencarta Posted March 2, 2010 Author Share Posted March 2, 2010 No, I'm not seeing the text I entered in bold. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 2, 2010 Share Posted March 2, 2010 It looks like part of your if statement doesn't include an echo or print statement. Step through your logic, you'll see there's a case where no output gets printed. Link to comment Share on other sites More sharing options...
Mencarta Posted March 2, 2010 Author Share Posted March 2, 2010 I did, but it still doesn't work. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 2, 2010 Share Posted March 2, 2010 Post the code you're using now, use a regular tag so hopefully it leaves the tags in. At least I think it should.[code][b]text[/b] Apparently so. Link to comment Share on other sites More sharing options...
Mencarta Posted March 2, 2010 Author Share Posted March 2, 2010 <html> <head> <title>Server - PHP - FML</title> <style> a, a:link, a:visited, a:hover { color:blue; } ul { list-style-type:none; position:relative; left:-17px; } </style> </head> <body> <center> <h1>Server - PHP - FML</h1> <p>Please enter some text that has the text "[b]" and "[/b]".</p> <br /> <?php if (!isset($_POST["text"])) { ?> <form action="http://localhost/php/fml.php" method="post"> <input type="text" name="text" /> <br /> <input type="submit" value="Submit" /> </form> <?php } if (isset($_POST["text"])) { if (strstr($_POST["text"],"[b]")) { if (strstr($_POST["text"],"[/b]")) { $text = str_replace("[b]","<b>",$text); $text = str_replace("[/b]","</b>",$text); echo $text; } else { $_POST["text"] += "[/b]"; $text = str_replace("[b]","<b>",$text); $text = str_replace("[/b]","</b>",$text); echo $text; } } else { echo $_POST["text"]; } } ?> </center> </body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted March 2, 2010 Share Posted March 2, 2010 Whoops, I missed it before. You're not setting $text to anything, it's an undefined variable when you try to use it. You could actually just replace this:$text = str_replace("","<b>",$text);with this:$text = str_replace("","<b>",$_POST['text']);Not that it's an error, but you could also minimize your if statement a little. You could swap this: if (strstr($_POST["text"],"[/b]")) { $text = str_replace("[b]","<b>",$text); $text = str_replace("[/b]","</b>",$text); echo $text; } else { $_POST["text"] += "[/b]"; $text = str_replace("[b]","<b>",$text); $text = str_replace("[/b]","</b>",$text); echo $text; } with this: if (strstr($_POST["text"],"[/b]") === false) { $_POST["text"] .= "[/b]"; } $text = str_replace("[b]","<b>",$_POST['text']); $text = str_replace("[/b]","</b>",$text); echo $text; You run those same 3 lines regardless of what happens, so might as well write them once.I missed this too, but you were using += when you should have been using .=, the plus operator in PHP is just for addition. Link to comment Share on other sites More sharing options...
Mencarta Posted March 2, 2010 Author Share Posted March 2, 2010 I did that, but it will not change the to <b>. My code:fml.php <html> <head> <title>Server - PHP - FML</title> <style> a, a:link, a:visited, a:hover { color:blue; } ul { list-style-type:none; position:relative; left:-17px; } </style> </head> <body> <center> <h1>Server - PHP - FML</h1> <p>Please enter some text that has the text "[b]" and "[/b]".</p> <br /> <?php if (!isset($_POST["text"])) { ?> <form action="http://localhost/php/fml.php" method="post"> <input type="text" name="text" /> <br /> <input type="submit" value="Submit" /> </form> <?php } if (isset($_POST["text"])) { if (strstr($_POST["text"],"[b]")) { if (strstr($_POST["text"],"[/b]")) { $text = str_replace("[b]","<b>",$_POST["text"]); $text = str_replace("[/b]","</b>",$_POST["text"]); echo $text; } else { $_POST["text"] .= "[/b]"; $text = str_replace("[b]","<b>",$_POST["text"]); $text = str_replace("[/b]","</b>",$_POST["text"]); echo $text; } } else { echo $_POST["text"]; } } ?> </center> </body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted March 2, 2010 Share Posted March 2, 2010 strstr may not pick up on the text. If the text you're looking for is at the end of the string, strstr will return an empty string which will evaluate to false. So, if "" or "" shows up at the end of the string your if statement will not catch it. If you're checking if a string is in another string, use strpos instead. So you can replace this line:if (strstr($_POST["text"],""))with this:if (strpos($_POST["text"],"") !== false)There's another line using strstr that you can change the same way. Link to comment Share on other sites More sharing options...
Mencarta Posted March 3, 2010 Author Share Posted March 3, 2010 Thanks, I took all your suggestions. Link to comment Share on other sites More sharing options...
Mencarta Posted March 3, 2010 Author Share Posted March 3, 2010 Now the real challenge italics and bold. I got italics working, but when I try to use it in the same field it does the italics and bold, but the tags are left in. Here is my code:fml.php <html> <head> <title>Server - PHP - FML</title> <style> a, a:link, a:visited, a:hover { color:blue; } ul { list-style-type:none; position:relative; left:-17px; } </style> </head> <body> <center> <h1>Server - PHP - FML</h1> <p>Please enter some text with using the following format:</p> <ul> <li><b>[b]</b> and <b>[/b]</b> for bold</li> <li><b>[i]</b> and <b>[/i]</b> for italics</li> </ul> <br /> <?php if (!isset($_POST["text"])) { ?> <form action="http://localhost/php/fml.php" method="post"> <input type="text" name="text" /> <br /> <input type="submit" value="Submit" /> </form> <?php } if (isset($_POST["text"])) { if (strpos($_POST["text"],"[b]") !== false) { if (strpos($_POST["text"],"[/b]") === false) { $_POST["text"] .= "[/b]"; } $text = str_replace("[b]","<b>",$_POST["text"]); $text = str_replace("[/b]","</b>",$text); echo $text; } if (strpos($_POST["text"],"[i]") !== false) { if (strpos($_POST["text"],"[/i]") === false) { $_POST["text"] .= "[/i]"; } $text = str_replace("[i]","<i>",$_POST["text"]); $text = str_replace("[/i]","</i>",$text); echo $text; } } else { echo $text; } ?> </center> </body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted March 3, 2010 Share Posted March 3, 2010 I think it's related to how $text keeps getting set from $_POST. So instead of mixing $text and $_POST['text'], it's probably better to set $text once then only use that. Also, instead of echoing the text after replacing either the bold or italic, replace everything and then echo once. if (isset($_POST["text"])) { $text = $_POST['text']; if (strpos($text,"[b]") !== false) { if (strpos($text,"[/b]") === false) { $text .= "[/b]"; } $text = str_replace("[b]","<b>",$text); $text = str_replace("[/b]","</b>",$text); } if (strpos($text,"[i]") !== false) { if (strpos($text,"[/i]") === false) { $text .= "[/i]"; } $text = str_replace("[i]","<i>",$text); $text = str_replace("[/i]","</i>",$text); } echo $text; } I'm not trying to discourage you from parsing the text yourself, because it's a good exercise, but PHP also has built-in support for working with bbcode specifically:http://www.php.net/manual/en/function.bbcode-create.php Link to comment Share on other sites More sharing options...
Mencarta Posted March 3, 2010 Author Share Posted March 3, 2010 Thanks, I realized echoing each time was the problem after I posted this and went to sleep. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.