How to slice a string in PHP?

2019-02-15 07:50发布

问题:

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.

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 />";
    }
}
?>

回答1:

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...



回答2:

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.



回答3:

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.



回答4:

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"


标签: php string slice