Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 搜索和搜索的问题;使用substr\u replace替换_Php_String_Search_Replace - Fatal编程技术网

Php 搜索和搜索的问题;使用substr\u replace替换

Php 搜索和搜索的问题;使用substr\u replace替换,php,string,search,replace,Php,String,Search,Replace,我试过很多方法,有些部分有效,有些根本不起作用。这一个根本不起作用,因为while条件由于某种原因一直返回false,它不会开始替换。 我基本上想要的是我放入一个字符串,然后我搜索一个单词并用另一个单词替换它。我成功地做到了这一点,唯一的问题不是替换从字符串第0个位置开始的单词,它仍然在回显字符串的第0个字母,并用新单词继续。例如:“旧的就是旧的”,我想用新的代替旧的,它会呼应“新的就是新的”。 请告诉我,如果我应该做什么不同的,以及为了有一个更干净和完美的优化代码加快网站。 多谢各位 <

我试过很多方法,有些部分有效,有些根本不起作用。这一个根本不起作用,因为while条件由于某种原因一直返回false,它不会开始替换。 我基本上想要的是我放入一个字符串,然后我搜索一个单词并用另一个单词替换它。我成功地做到了这一点,唯一的问题不是替换从字符串第0个位置开始的单词,它仍然在回显字符串的第0个字母,并用新单词继续。例如:“旧的就是旧的”,我想用新的代替旧的,它会呼应“新的就是新的”。 请告诉我,如果我应该做什么不同的,以及为了有一个更干净和完美的优化代码加快网站。 多谢各位

<?php 
$offset = 0;
if (isset($_POST['user_input']) && !empty ($_POST['user_input'])) {
    $initial_string = $_POST['user_input'];
    $string_length = strlen($initial_string);
    $new_string = $initial_string;
    if (isset($_POST['search_input']) && !empty ($_POST['search_input'])) {
        $search_input = $_POST['search_input'];
        $search_input_length = strlen($search_input);
    } else {
        echo 'Please write the string that you want to replace into the Search input'.'<br>'.PHP_EOL;
    }
    if (isset($_POST['replace_input']) && !empty ($_POST['replace_input'])) {
        $replace_input = $_POST['replace_input'];
        $replace_input_length = strlen($replace_input);
    } else {
        echo 'Please write the string that you want to switch to into the Replace input'.'<br>'.PHP_EOL;
    }
    while (strpos($new_string,$search_input,$offset) === true) {
    $strpos = strpos($new_string,$search_input,$offset);
    if ($offset<$string_length) {
            $new_string = substr_replace($new_string,$replace_input,$strpos,$search_input_length);
            $offset = $offset + $replace_input_length;
        } else {
            break;
        }
    }
}

echo $new_string;

?>

<hr>

<form action="index.php" method="POST">
    <textarea name="user_input" rows="7" cols="30"></textarea><Br>
    Search: <input type="value" name="search_input"><br>
    Replace: <input type="value" name="replace_input"><br>
    <input type="submit" value="submit">

</form>



搜索:
替换:

如果我知道您想用另一个子字符串替换给定子字符串(在较大字符串中)的所有匹配项

为此,您只需使用
str\u replace

while
循环替换为

$new_string = str_replace( $search_input, $replace_input, $initial_string );

可能我不知道我不知道你到底想做什么,但这似乎有点太复杂了。为什么不在整个用户输入上运行一个str_替换

if (!empty($_POST['user_input'] && !empty ($_POST['search_input'] && !empty ($_POST['replace_input'])) {
    $str = str_replace($_POST['search_input'], $_POST['replace_input'], $_POST['user_input']);
    die(var_dump($str));
} else {
    die('error');
}

你的代码有很多错误。以下是一些需要注意的重要事项:

  • isset($var)&&!空($var)
    是多余的
    empty($var)
    还检查变量是否已设置,如果未设置,则返回true。只要
    !空($var)
    就足够了

  • 您正在检查
    strpos()
    是否返回布尔值
    true
    strpos()
    从不返回
    true
    。它要么返回针在干草堆中的位置,要么返回
    false
    (如果在干草堆中找不到针)

正在修复当前代码 更改
while
条件以检查
strpos()
是否返回非假值(在找到匹配项时为这种情况):

更好的解决方案 您当前的代码似乎过于复杂。实际上,您只是尝试替换字符串中出现的所有子字符串。这正是
str_replace()
所做的。改用这个函数。然后,您的代码可以简化为:

if (validation goes here) {
    $new_string = str_replace($search_input, $replace_input, $new_string);
}

好的,现在我明白我所有的错误,只有一个。有什么区别?在哪些情况下应该使用substr\u replace,在哪些情况下应该使用str\u replace?@Maniak:要替换特定部分中的文本,请使用
substr\u replace()
。对于其他内容,请使用
str\u replace()
new is new
if (validation goes here) {
    $new_string = str_replace($search_input, $replace_input, $new_string);
}