【问题标题】:Scan string for first occurrence of certain word and replace word plus next few characters with php扫描字符串以查找特定单词的第一次出现并用php替换单词加上接下来的几个字符
【发布时间】:2016-11-08 13:22:12
【问题描述】:

我有一个字符串:

"This is the string. My height="200" and width="200".The second height="300" and width ="300""

如何扫描字符串并获取第一个高度元素并替换它?这意味着我只想抓住"height="200"" 并替换它或完全删除它,但是我如何扫描文件以查找第一次出现的位置? 同样对于我正在使用的实际字符串,我不知道高度设置为多少,所以我不能只搜索它。我想我需要找到"height=" 并在它之后获取接下来的几个字符并进行更改。 我知道我可以使用以下方法进行搜索:

function str_replace_limit($search, $replace, $string, $limit = 1) {
            $pos = strpos($string, $search);

            if ($pos === false) {
                return $string;
            }

            $searchLen = strlen($search);

            for ($i = 0; $i < $limit; $i++) {
                $string = substr_replace($string, $replace, $pos, $searchLen);

                $pos = strpos($string, $search);

                if ($pos === false) {
                    break;
                }
            }

            return $string;
        }
     $search  = 'height';
        $replace = ' ';
        $string  = "This is the string. My height="200" and width="200".The second height="300" and width ="300"";


        $limit   = 1;

        $replaced = str_replace_limit(($search), $replace, $string, $limit);

返回:

This is the string. My ="200" and width="200".The second height="300" and width ="300"

找到第一个高度元素但我无法获取它之后的字符? 有任何想法吗? 提前致谢!

【问题讨论】:

    标签: php string search replace


    【解决方案1】:

    您可以使用preg_replace() 和一些正则表达式轻松做到这一点。使用preg_replace() 的限制选项(每个主题字符串中每个模式的最大可能替换。默认为-1,没有限制)可以帮助您限制返回的匹配数。将限制设置为 1 会返回第一个匹配项:

    $subject = 'This is the string. My height="200" and width="200".The second height="300" and width ="300"';
    $search = '/(height=\")(\d+)(\")/';
    $replace = 'height="450"';
    $new = preg_replace($search, $replace, $subject, 1);
    echo $new;
    

    返回:

    这是字符串。我的 height="450" and width="200". 第二个 height="300" and width="300"

    正则表达式的解释:

    • 第一捕获组(height=\") -- height= 与字符 height= 逐字匹配(区分大小写) -- \" 匹配字符 " 字面意思

    • 第二个捕获组(\d+) -- \d+ 匹配一个数字 [0-9] -- 量词:+ 在一次和无限次之间,多次 可能,按需回馈[贪婪]

    • 第三个捕获组(\") -- \" 匹配字符 " 字面意思

    为了测试正则表达式,我使用了 regex101.com 来确保 the regex shown 有效。使用 regex101.com 的注意事项 - 始终应用 \g 修饰符(用于全局匹配),因为 PHP 在使用 regex 时几乎总是贪婪。

    【讨论】:

      【解决方案2】:

      您正在搜索简单的文本,但没有告诉您的函数它应该搜索更多内容以及这个“更多”的样子。 要实现这一点,您需要使用描述为herepreg_repalce 函数。你可以用这样的东西替换你的函数

      function str_replace_limit($search, $replace, $string, $limit = 1) 
      {
          $pattern = '/' . $search . '="(.*?)"/';
      
          return preg_replace($pattern, $replace, $string, $limit);
      }
      

      【讨论】:

        【解决方案3】:

        使用带有limit 标志的preg_replace 函数可以轻松执行所需的替换(每个主题字符串中每个模式的最大可能替换。默认为-1(无限制)。)

        $string  = 'This is the string. My height="200" and width="200".The second height="300" and width ="300"';
        
        $replaced = preg_replace("/ height=[\"']\w+?[\"']/", " ", $string, 1);
        
        print_r($replaced);
        // This is the string. My  and width="200".The second height="300" and width ="300"
        

        【讨论】:

        • 感谢您的解决方案!效果很好!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-30
        • 1970-01-01
        • 2019-08-08
        • 2018-02-06
        • 2022-07-05
        相关资源
        最近更新 更多