How to slice a string in PHP?

做~自己de王妃 提交于 2019-11-30 23:43:25

You can either use explode() (http://php.net/explode) or a mix of substr() (http://php.net/substr) with strpos() (http://php.net/strpos).

<?php
$string = "MICROSOFT CORP CIK#: 0000789019 (see all company filings)";
$newString = substr($string, 0, strpos($string, " CIK#"));
echo $newString;

Edit: edited a few times to fit your question editing...

You 'd find the position of "CORP" with strpos (be sure to read the giant red warning) and then cut off the relevant part with substr.

Assuming your string is stored in $a, then any of

echo substr($a, 0, strpos($a, " CIK"));

or

preg_match("/(.*) CIK/", $a, $matches);
echo $matches[1];

or

echo preg_replace("/(.*) CIK.*/", "$1", $a);

will do.

I came to this page looking for a slice slice($start, $end) method, but only found case-specific solutions.

In my case, I only have indexes (start and end). The need for a length to slice a string seemed silly. So I wrote a slice function. It mimics JavaScript's slice method.

// str_slice(string $str, int $start [, int $end])
function str_slice() {
    $args = func_get_args();
    switch (count($args)) {
        case 1:
            return $args[0];
        case 2:
            $str        = $args[0];
            $str_length = strlen($str);
            $start      = $args[1];
            if ($start < 0) {
                if ($start >= - $str_length) {
                    $start = $str_length - abs($start);
                } else {
                    $start = 0;
                }
            }
            else if ($start >= $str_length) {
                $start = $str_length;
            }
            $length = $str_length - $start;
            return substr($str, $start, $length);
        case 3:
            $str        = $args[0];
            $str_length = strlen($str);
            $start      = $args[1];
            $end        = $args[2];
            if ($start >= $str_length) {
                return "";
            }
            if ($start < 0) {
                if ($start < - $str_length) {
                    $start = 0;
                } else {
                    $start = $str_length - abs($start);
                }
            }
            if ($end <= $start) {
                return "";
            }
            if ($end > $str_length) {
                $end = $str_length;
            }
            $length = $end - $start;
            return substr($str, $start, $length);
    }
    return null;
}


var_dump( str_slice("abcdefghijklmnopqrstuvwxyz")          ); // "abcdefghijklmnopqrstuvwxyz"
var_dump( str_slice("abcdefghijklmnopqrstuvwxyz", 5)       ); // "fghijklmnopqrstuvwxyz"
var_dump( str_slice("abcdefghijklmnopqrstuvwxyz", -5)      ); // "vwxyz"
var_dump( str_slice("abcdefghijklmnopqrstuvwxyz", 40)      ); // ""
var_dump( str_slice("abcdefghijklmnopqrstuvwxyz", -40)     ); // "abcdefghijklmnopqrstuvwxyz"
var_dump( str_slice("abcdefghijklmnopqrstuvwxyz", 5, 10)   ); // "fghij"
var_dump( str_slice("abcdefghijklmnopqrstuvwxyz", 5, 20)   ); // "fghijklmnopqrst"
var_dump( str_slice("abcdefghijklmnopqrstuvwxyz", 5, 30)   ); // "fghijklmnopqrstuvwxyz"
var_dump( str_slice("abcdefghijklmnopqrstuvwxyz", -20, 2)  ); // ""
var_dump( str_slice("abcdefghijklmnopqrstuvwxyz", -20, 10) ); // "ghij"
var_dump( str_slice("abcdefghijklmnopqrstuvwxyz", -20, 15) ); // "ghijklmno"
var_dump( str_slice("abcdefghijklmnopqrstuvwxyz", -20, 40) ); // "ghijklmnopqrstuvwxyz"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!