Jump to content

[SOLVED] Help me please bolding and underlineing text


dude32

Recommended Posts

I'm a beginner to PHP and I am trying to write a PHP script that takes a text string, bolds and/or underlines the text string, and outputs the altered string to the monitor and allow me to select the alteration of the string I desire. I have tried repeatedly and I think my code is just garbage so if somebody could post me an example of this it would be of great help.  Thank you

We can, to put a string in a tag, prepend and append text to it. Like so:

 

<?php
$string = 'This will be bold';
$boldstring = "<b>$string</b>";

 

If you were to echo $boldstring you would get the text in bold. Now let's say you want to make a script to do this (I'm not sure why you are trying to do this, but at first guess you're building some kind of bbcode system) that would take input and then output. Let's say we'll do bold, underline, and italics.

 

<?php

if($_POST) //If form was submitted
{
$a = $_POST['a']; //This will be the 'action' variable that tells us what we need to do.
$s = $_POST['s']; //This will be the string variable that holds the string we will manipulate.

if(!$s)
{
	$s = '<i>(no data)</i>';
}
switch($a) //Make a switch to handle what action we get
{
	case "b":
		echo "<b>$s</b>";
	break;

	case "i":
		echo "<i>$s</i>";
	break;

	case "u":
		echo "<u>$s</u>";
	break;

	default: 
		die("Invalid selection.");
	break;
}

echo "<br /><br />";
}
?>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='POST'>
<select name='a'>
<option value='b'><b>Bold</b></option>
<option value='i'><i>Italics</i></options>
<option value='u'><u>Underline</u></options>
</select>
<br />
<input type='text' name='s'>
<br />
<input type='submit' value='Submit'>
</form>

That is tested and it should work. I am curious what you are trying to accomplish by this?

Archived

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

×
×
  • 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.