如何在 PHP 中对字符串进行切片?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7697459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:12:15  来源:igfitidea点击:

How to slice a string in PHP?

phpstringslice

提问by Cecil Rodriguez

Ok, so I've got this string:

好的,所以我有这个字符串:

"MICROSOFT CORP CIK#: 0000789019 (see all company filings)"

And I would like to cut off everything after the "CORP"bit. How would I go about doing this in PHP? I am used to Python so I am not sure how this is done.

我想"CORP"在位之后切断一切。我将如何在 PHP 中执行此操作?我习惯了 Python,所以我不确定这是如何完成的。

To be clear, this is the output I want:

需要明确的是,这是我想要的输出:

"MICROSOFT CORP"

I am trying:

我在尝试:

$companyname = substr($companyname, 0, strpos($companyname, " CIK"));

and I am getting nothing showing.

我什么也没显示。

Here is my full code:

这是我的完整代码:

<?php
include 'simple_html_dom.php';
$html = file_get_html('http://www.sec.gov/cgi-bin/browse-edgar?company=&match=&CIK=MSFT&filenum=&State=&Country=&SIC=&owner=exclude&Find=Find+Companies&action=getcompany');
$companyname = $html->find('span[class=companyName]', 0);
$companyname = substr($companyname, 0, strpos($companyname, " CIK#")+5);
$bizadd = $html->find('div[class="mailer"]');
echo $companyname;
echo "<br />";
foreach ($bizadd as $value) {
    $addvals = $value->find('span[class="mailerAddress"]');
    echo "<br />";
    foreach ($addvals as $value) {
        echo $value;
        echo "<br />";
    }
}
?>

回答by Dvir Azulay

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

您可以使用explode()( http://php.net/explode) 或substr()( http://php.net/substr) 与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...

编辑:编辑了几次以适合您的问题编辑...

回答by Jon

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.

你会找到"CORP"with的位置strpos(一定要阅读巨大的红色警告),然后用 切断相关部分substr

回答by akinuri

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

我来到这个页面寻找切片slice($start, $end)方法,但只找到了特定于案例的解决方案。

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

就我而言,我只有索引(开始和结束)。需要对length字符串进行切片似乎很愚蠢。所以我写了一个切片函数。它模仿了 JavaScript 的slice方法。

// 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"

回答by JRL

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

假设您的字符串存储在 $a 中,那么任何

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

or

或者

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

or

或者

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

will do.

会做。