获取字符串的子串
Get a substring of a string
我一直在尝试获取包含大约 40 行文本的字符串的子字符串。
字符串是这样的,但带有近似值。还有 30 行:
Username: example
Password: pswd
Code: 890382
Key: 9082
type: 1
Website: https://example.com/example
Email: example@example.com
我需要获取 Code
的值,这将是 890382
,但我似乎做不到。
像 Code
或 Key
这样的每个字段在字符串中都是唯一的。最好的(如果可能的话)是读取值并将它们存储在一个数组中,数组中的位置以字段命名。如果有人可以帮助我,我将不胜感激。
顺便说一句:此文件托管在我只能读取的另一台服务器上,因此我无法将其更改为 CSV 之类的内容。
我尝试使用的代码:
$begin=strpos($output, 'Code: ');
$end=strpos($output, '<br>', $begin);
$sub=substr($output, $begin, $end);
echo $sub;
取Username:的结束位置,然后找到Password的开始位置。然后使用这些值来提取用户名值。像这样找到你想要的值...
假设你的字符串是用换行符分隔的,你可以试试这个:
$str = "Username: example
Password: pswd
Code: 890382
Key: 9082
type: 1
Website: https://example.com/example
Email: example@example.com";
$explode = explode("<br/>", $str);
foreach ($explode as $string) {
$nextExplode = explode(":", $str);
foreach($nextExplode as $nextString) {
if ($nextString[0] == 'Code']) {
echo $nextString[1];
}
}
}
尝试使用拆分定界符“\n”and/or ':',这将为您提供一个数组,您可以在其中进一步分解成键值对。
在下面的例子中,我采用了从文件中读取的方法,并在给定的一行中用“:\s”分割。
即。 'data.txt'
的示例
<?php
$results = array();
$file_handle = fopen("data.txt", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
$line_array = preg_split("/:\s/", $line);
// validations ommited
$key = $line_array[0];
$value = $line_array[1];
// ie. $result['Code'] => '890382'
$result[$key] = $value;
}
fclose($file_handle);
print_r($result);
?>
输出用法ie.echo $result['Username']:
Array
(
[Username] => example
[Password] => pswd
[Code] => 890382
[Key] => 9082
[type] => 1
[Website] => https://example.com/example
[Email] => example@example.com
)
你可以试试
preg_match('/Code: ([0-9]+)/', $subject, $matches);
您应该在 $matches 数组中有代码。
您应该调整正则表达式以适合您的情况。我只是举个例子。
在您的特殊情况下使用以下代码:
preg_match('/Code\:\s*(.*)\s*/m', $yourstring, $match);
$match[1] //contains your code!
拆分每行,然后拆分冒号。并将 key/pairs 放入数组中:
$string = "..."; // your string
$lines = explode("\n",str_replace("\r","\n",$string)); // all forms of new lines
foreach ($lines as $line) {
$pieces = explode(":", $line, 2); // allows the extra colon URLs
if (count($pieces) == 2) { // skip empty and malformed lines
$values[trim($pieces[0])] = trim($pieces[1]); // puts keys and values in array
}
}
现在您可以通过访问$values['Code']
来获取您的价值
这应该适合你:
这里我先explode()
your string by a new line character. After this I go through each element with array_map()
,然后再爆:
。然后我简单地 array_combine()
第一个数组列和第二列,这是我用 array_column()
得到的。
<?php
$str = "Username: example
Password: pswd
Code: 890382
Key: 9082
type: 1
Website: https://example.com/example
Email: example@example.com";
$arr = array_map(function($v){
return array_map("trim", explode(":", $v, 2));
}, explode(PHP_EOL, $str));
$arr = array_combine(array_column($arr, 0), array_column($arr, 1));
print_r($arr);
?>
输出:
Array
(
[Username] => example
[Password] => pswd
[Code] => 890382
[Key] => 9082
[type] => 1
[Website] => https://example.com/example
[Email] => example@example.com
)
我一直在尝试获取包含大约 40 行文本的字符串的子字符串。
字符串是这样的,但带有近似值。还有 30 行:
Username: example
Password: pswd
Code: 890382
Key: 9082
type: 1
Website: https://example.com/example
Email: example@example.com
我需要获取 Code
的值,这将是 890382
,但我似乎做不到。
像 Code
或 Key
这样的每个字段在字符串中都是唯一的。最好的(如果可能的话)是读取值并将它们存储在一个数组中,数组中的位置以字段命名。如果有人可以帮助我,我将不胜感激。
顺便说一句:此文件托管在我只能读取的另一台服务器上,因此我无法将其更改为 CSV 之类的内容。
我尝试使用的代码:
$begin=strpos($output, 'Code: ');
$end=strpos($output, '<br>', $begin);
$sub=substr($output, $begin, $end);
echo $sub;
取Username:的结束位置,然后找到Password的开始位置。然后使用这些值来提取用户名值。像这样找到你想要的值...
假设你的字符串是用换行符分隔的,你可以试试这个:
$str = "Username: example
Password: pswd
Code: 890382
Key: 9082
type: 1
Website: https://example.com/example
Email: example@example.com";
$explode = explode("<br/>", $str);
foreach ($explode as $string) {
$nextExplode = explode(":", $str);
foreach($nextExplode as $nextString) {
if ($nextString[0] == 'Code']) {
echo $nextString[1];
}
}
}
尝试使用拆分定界符“\n”and/or ':',这将为您提供一个数组,您可以在其中进一步分解成键值对。
在下面的例子中,我采用了从文件中读取的方法,并在给定的一行中用“:\s”分割。
即。 'data.txt'
的示例<?php
$results = array();
$file_handle = fopen("data.txt", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
$line_array = preg_split("/:\s/", $line);
// validations ommited
$key = $line_array[0];
$value = $line_array[1];
// ie. $result['Code'] => '890382'
$result[$key] = $value;
}
fclose($file_handle);
print_r($result);
?>
输出用法ie.echo $result['Username']:
Array
(
[Username] => example
[Password] => pswd
[Code] => 890382
[Key] => 9082
[type] => 1
[Website] => https://example.com/example
[Email] => example@example.com
)
你可以试试
preg_match('/Code: ([0-9]+)/', $subject, $matches);
您应该在 $matches 数组中有代码。
您应该调整正则表达式以适合您的情况。我只是举个例子。
在您的特殊情况下使用以下代码:
preg_match('/Code\:\s*(.*)\s*/m', $yourstring, $match);
$match[1] //contains your code!
拆分每行,然后拆分冒号。并将 key/pairs 放入数组中:
$string = "..."; // your string
$lines = explode("\n",str_replace("\r","\n",$string)); // all forms of new lines
foreach ($lines as $line) {
$pieces = explode(":", $line, 2); // allows the extra colon URLs
if (count($pieces) == 2) { // skip empty and malformed lines
$values[trim($pieces[0])] = trim($pieces[1]); // puts keys and values in array
}
}
现在您可以通过访问$values['Code']
这应该适合你:
这里我先explode()
your string by a new line character. After this I go through each element with array_map()
,然后再爆:
。然后我简单地 array_combine()
第一个数组列和第二列,这是我用 array_column()
得到的。
<?php
$str = "Username: example
Password: pswd
Code: 890382
Key: 9082
type: 1
Website: https://example.com/example
Email: example@example.com";
$arr = array_map(function($v){
return array_map("trim", explode(":", $v, 2));
}, explode(PHP_EOL, $str));
$arr = array_combine(array_column($arr, 0), array_column($arr, 1));
print_r($arr);
?>
输出:
Array
(
[Username] => example
[Password] => pswd
[Code] => 890382
[Key] => 9082
[type] => 1
[Website] => https://example.com/example
[Email] => example@example.com
)