Jump to content

Fml


Mencarta

Recommended Posts

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

<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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...