Используя strpos(), я не получаю желаемых результатов. Я подозреваю, что проблема в условных операторах. Если условие верно, то вроде все работает нормально. Но если оно ложно, код для условия true все равно выполняется. Вот код.
<?php
// require_once 'functions/functions.php';
?>
<?php
if (isset($_POST['submit'])) {
$string = $_POST['sentence'];
$findString = $_POST['findstring'];
$strPosition = stripos($string, stringToFind($findString));
// if (($strPosition == true) || ($strPosition == 0)) {
if ($strPosition !== true) {
echo 'Found!', '<br><br>';
echo 'In the string ', $string, '.', '<br>';
echo 'And the word you want to find is ';
$readStr = substr($string, $strPosition, strlen($findString));
echo $readStr, '.', '<br>';
if ($strPosition == 0) {
echo 'It is at the beginning of the string.', '<br>';
}
else {
echo 'It is in the ', $strPosition, ' ', 'position.', '<br>';
}
}
else {
echo 'Not found. Try again.', '<br>';
}
}
function stringToFind($findString)
{
return $findString = $findString;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>String Position</title>
</head>
<body>
<h1>Finding a string and then read it</h1><br><br>
<form id="form1" class="form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<label for="sentence">Sentence here:
<textarea id="sentence" name="sentence" value="Put a sentence here."></textarea></label>
Enter a string: <input type="text" name="findstring">
<input type="submit" name="submit" value="Go">
</form><br><br>
</body>
</html>
if ($strPosition !== false)
должно быть условием. - person Ja͢ck   schedule 04.07.2014