• <bdo id='d7LDN'></bdo><ul id='d7LDN'></ul>
  1. <i id='d7LDN'><tr id='d7LDN'><dt id='d7LDN'><q id='d7LDN'><span id='d7LDN'><b id='d7LDN'><form id='d7LDN'><ins id='d7LDN'></ins><ul id='d7LDN'></ul><sub id='d7LDN'></sub></form><legend id='d7LDN'></legend><bdo id='d7LDN'><pre id='d7LDN'><center id='d7LDN'></center></pre></bdo></b><th id='d7LDN'></th></span></q></dt></tr></i><div id='d7LDN'><tfoot id='d7LDN'></tfoot><dl id='d7LDN'><fieldset id='d7LDN'></fieldset></dl></div>
    1. <small id='d7LDN'></small><noframes id='d7LDN'>

      <tfoot id='d7LDN'></tfoot>

      <legend id='d7LDN'><style id='d7LDN'><dir id='d7LDN'><q id='d7LDN'></q></dir></style></legend>
    2. 删除字符串的一部分,但仅当它位于字符串末尾

      时间:2023-07-31

          <small id='sNjuQ'></small><noframes id='sNjuQ'>

        • <i id='sNjuQ'><tr id='sNjuQ'><dt id='sNjuQ'><q id='sNjuQ'><span id='sNjuQ'><b id='sNjuQ'><form id='sNjuQ'><ins id='sNjuQ'></ins><ul id='sNjuQ'></ul><sub id='sNjuQ'></sub></form><legend id='sNjuQ'></legend><bdo id='sNjuQ'><pre id='sNjuQ'><center id='sNjuQ'></center></pre></bdo></b><th id='sNjuQ'></th></span></q></dt></tr></i><div id='sNjuQ'><tfoot id='sNjuQ'></tfoot><dl id='sNjuQ'><fieldset id='sNjuQ'></fieldset></dl></div>

        • <legend id='sNjuQ'><style id='sNjuQ'><dir id='sNjuQ'><q id='sNjuQ'></q></dir></style></legend>

            <tfoot id='sNjuQ'></tfoot>

                <bdo id='sNjuQ'></bdo><ul id='sNjuQ'></ul>
                  <tbody id='sNjuQ'></tbody>
                本文介绍了删除字符串的一部分,但仅当它位于字符串末尾时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我需要删除字符串的子字符串,但前提是它位于字符串的末尾.

                I need to remove a substring of a string, but only when it is at the END of the string.

                例如,删除以下字符串末尾的 'string' :

                for example, removing 'string' at the end of the following strings :

                "this is a test string" ->  "this is a test "
                "this string is a test string" - > "this string is a test "
                "this string is a test" -> "this string is a test"
                

                有什么想法吗?可能是某种 preg_replace,但如何??

                Any idea's ? Probably some kind of preg_replace, but how??

                推荐答案

                您会注意到 $ 字符的使用,它表示字符串的结尾:

                You'll note the use of the $ character, which denotes the end of a string:

                $new_str = preg_replace('/string$/', '', $str);
                

                如果字符串是用户提供的变量,最好先通过 preg_quote 运行它:

                If the string is a user supplied variable, it is a good idea to run it through preg_quote first:

                $remove = $_GET['remove']; // or whatever the case may be
                $new_str = preg_replace('/'. preg_quote($remove, '/') . '$/', '', $str);
                

                这篇关于删除字符串的一部分,但仅当它位于字符串末尾时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:不推荐使用:函数 eregi_replace() 下一篇:如何在 PHP 中的变量中用空格替换制表符?

                相关文章

                最新文章

                <legend id='CkVXJ'><style id='CkVXJ'><dir id='CkVXJ'><q id='CkVXJ'></q></dir></style></legend>

                  <small id='CkVXJ'></small><noframes id='CkVXJ'>

                    <bdo id='CkVXJ'></bdo><ul id='CkVXJ'></ul>
                  <tfoot id='CkVXJ'></tfoot>
                1. <i id='CkVXJ'><tr id='CkVXJ'><dt id='CkVXJ'><q id='CkVXJ'><span id='CkVXJ'><b id='CkVXJ'><form id='CkVXJ'><ins id='CkVXJ'></ins><ul id='CkVXJ'></ul><sub id='CkVXJ'></sub></form><legend id='CkVXJ'></legend><bdo id='CkVXJ'><pre id='CkVXJ'><center id='CkVXJ'></center></pre></bdo></b><th id='CkVXJ'></th></span></q></dt></tr></i><div id='CkVXJ'><tfoot id='CkVXJ'></tfoot><dl id='CkVXJ'><fieldset id='CkVXJ'></fieldset></dl></div>