与使用php中的位置的子串相反

时间:2012-03-16 16:18:59

标签: php

就像我使用

一样
  substr($sometext, 4, 10) 

选择字符串的某个部分,如何使用精确定位从字符串中删除某个部分?

5 个答案:

答案 0 :(得分:2)

您可以使用两个子字符串:

$new_string = substr($sometext, 0, 4) . substr($sometext, 10);

答案 1 :(得分:2)

更简单的解决方案是将substr_replace与空替换字符串一起使用:

$new_string = substr_replace($sometext, '', 4, 10);

答案 2 :(得分:0)

$new_text = substr($sometext,0,4) . substr($sometext, 10, strlen($sometext));

答案 3 :(得分:0)

    $string = "Hello World";
    $output = substr($string, strpos($string, "W"));

如果要删除到最后一次出现,请使用strrpos而不是strpos。

答案 4 :(得分:0)

你可以使用jiggy并使用array_splice

$sometext = 'Hello, worlds! How are ya?';
echo str_splice($sometext, 4, 10); // o, worlds!


function str_splice($input, $offset, $length = 0) {
    $chars = str_split($input);
    return implode(array_splice($chars, $offset, $length));
}