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 />";
}
}
?>
The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string. rtrim() - Removes whitespace or other predefined characters from the right side of a string.
substr in PHP is a built-in function used to extract a part of the given string. The function returns the substring specified by the start and length parameter. It is supported by PHP 4 and above. Let us see how we can use substr() to cut a portion of the string.
explode() is a built in function in PHP used to split a string in different strings. The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs. This functions returns an array containing the strings formed by splitting the original string.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With