Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取字符串的子字符串_Php_String_Substring_Substr_Strpos - Fatal编程技术网

Php 获取字符串的子字符串

Php 获取字符串的子字符串,php,string,substring,substr,strpos,Php,String,Substring,Substr,Strpos,我一直在尝试获取一个包含大约40行文本的字符串的子字符串 字符串类似于此,但带有aprox。30多行: Username: example Password: pswd Code: 890382 Key: 9082 type: 1 Website: https://example.com/example Email: example@example.com 我需要得到的值,例如,code,它将是890382,但我似乎不能这样做 像code或Key这样的每个字段在字符串中都是唯一的。最好(如果可

我一直在尝试获取一个包含大约40行文本的字符串的子字符串

字符串类似于此,但带有aprox。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;
$begin=strpos($output,'code:');
$end=strpos($output,“
”,$begin); $sub=substr($output,$begin,$end); echo$sub;
获取用户名的结束位置:然后找到密码的起始位置。然后使用这些值提取用户名值。如下图所示,找到所需的值…

假设字符串由换行符分隔,您可以尝试以下操作:

$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];
             }
          }
     }
$str=“用户名:示例
密码:pswd
代码:890382
钥匙:9082
类型:1
网站:https://example.com/example
电邮:example@example.com";
$explode=explode(“
,$str”); foreach($分解为$string){ $nextExplode=explode(“:”,$str); foreach($nextExplode作为$nextString){ 如果($nextString[0]=='Code'])){ echo$nextString[1]; } } }
尝试使用拆分分隔符“\n”和/或“:”,这将为您提供一个数组,您可以在其中进一步分解为键值对

在下面的示例中,我采用了从文件中读取的方法,并根据“:\s”给定的行进行拆分

ie.带有“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);

?>
你可以试试

preg_match('/Code: ([0-9]+)/', $subject, $matches);
您应该将代码放在$matches数组中


您应该调整regexp,使其适合您的情况。我只是举个例子。

在您的特殊情况下,请使用以下代码:

preg_match('/Code\:\s*(.*)\s*/m', $yourstring, $match); 
$match[1] //contains your code! 

拆分每行,然后在冒号上拆分。并将密钥/对放入一个数组:

$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']

来获取您的值,这应该对您有用:

这里我首先用一个新行字符来表示字符串。在这之后,我使用遍历每个元素,然后通过
再次分解它。然后我简单地
array\u combine()
第一个数组列和第二个列,这是通过
array\u column()得到的


你试过什么吗?当然!为了得到子字符串的开头,我尝试了使用字符串作为草堆,字段名称作为指针的strpos,然后我尝试使用相同的东西,但使用strpos的先前结果作为开头,
和指针来获得结束位置。之后,我尝试使用substr来获得使用开始和结束位置的子字符串。它不起作用。。。(顺便说一句,我使用nl2br将\n转换为
)^然后向我们展示您的工作和尝试!显示你的代码。你的国家有谷歌吗@用户3676792只需回答您的问题并添加您的代码如果无法使用
进行分解,用户可以使用str_replace函数将所有换行符转换为空白。然后可以通过使用指针作为空格的爆炸函数进行操作。不是吗?这个代码永远不会起作用。首先,字符串中没有

。它不是html。第二步是爆炸,先切一根线,然后拿一块,把它当作一个阵列。太棒了!做了一些调整后,我想做的一切。谢谢
$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
    }
}
<?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
)