PHP Strings Demo
PHP Strings Tool
Results:
Text Length: 41 characters
Word Count: 8 words
New Text: Hello World! Welcome to the programming of PHP.
Text Length: 41 characters
Word Count: 8 words
New Text: Hello World! Welcome to the programming of PHP.
<?php
error_reporting(E_ALL);
include('includes/header.php');
// 1. Get user input with fallback defaults
$main_text = $_POST['main_text'] ?? 'Hello World! Welcome to the world of PHP.';
$search_word = $_POST['search_word'] ?? 'world';
$replace_word = $_POST['replace_word'] ?? 'programming';
// 2. Process the text
$text_length = strlen($main_text);
$word_count = str_word_count($main_text);
// 3. Condition: Check if the word exists in the text (case-sensitive)
// strpos() returns false if the word is not found
$word_found = (strpos($main_text, $search_word) !== false);
if ($word_found) {
$replaced_text = str_replace($search_word, $replace_word, $main_text);
} else {
$replaced_text = "Word not found! No changes were made.";
}
?>
<head>
<meta charset="UTF-8">
<title>PHP Strings Demo</title>
</head>
<body>
<h1>PHP Strings Demo</h1>
<h2>PHP Strings Tool</h2>
<!-- Form Section -->
<form action="" method="POST">
<p>
<label>Main Text:</label><br>
<textarea name="main_text" rows="4" cols="40" required><?= htmlspecialchars($main_text) ?></textarea>
</p>
<p>
<label>Find word:</label>
<input type="text" name="search_word" value="<?= htmlspecialchars($search_word) ?>" required>
</p>
<p>
<label>Replace with:</label>
<input type="text" name="replace_word" value="<?= htmlspecialchars($replace_word) ?>" required>
</p>
<input type="submit" value="Submit">
</form>
<hr>
<!-- Results Section -->
<h3>Results:</h3>
<p><strong>Text Length:</strong> <?= $text_length ?> characters</p>
<p><strong>Word Count:</strong> <?= $word_count ?> words</p>
<p><strong>New Text:</strong>
<?php if ($word_found): ?>
<?= htmlspecialchars($replaced_text) ?>
<?php else: ?>
<!-- Displayed in red if the word isn't found -->
<span style="color: red; font-style: italic;"><?= $replaced_text ?></span>
<?php endif; ?>
</p>
</body>
<?php
include('includes/footer.php');
?>