Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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从3行字符串中查找内容_Php_Regex_Preg Match - Fatal编程技术网

php从3行字符串中查找内容

php从3行字符串中查找内容,php,regex,preg-match,Php,Regex,Preg Match,全文如下: <tr> <td class="ttl"><a href="glossary.php3?term=dynamic-memory">Internal</a></td> <td class="nfo">16 GB, 1 GB RAM</td> </tr> 16 GB,1 GB内存 如何使用preg_match从文本中查找16 GB、1 GB RAM?您可以这样做: $str = '<

全文如下:

<tr>
<td class="ttl"><a href="glossary.php3?term=dynamic-memory">Internal</a></td>
<td class="nfo">16 GB, 1 GB RAM</td>
</tr>

16 GB,1 GB内存

如何使用preg_match从文本中查找
16 GB、1 GB RAM

您可以这样做:

$str = '<tr>
<td class="ttl"><a href="glossary.php3?term=dynamic-memory">Internal</a></td>
<td class="nfo">16 GB, 1 GB RAM</td>
</tr>';

preg_match('/\<td class="nfo"\>(.+?)\<\/td\>/', $str, $matches);

print_r($matches);
$str='1〕
16 GB,1 GB内存
';
预匹配('/\(.+?)\/',$str,$matches);
打印(匹配项);
结果:

Array
(
    [0] => <td class="nfo">16 GB, 1 GB RAM</td>
    [1] => 16 GB, 1 GB RAM
)
数组
(
[0]=>16 GB,1 GB RAM
[1] =>16 GB,1 GB RAM
)

哪项先前的研究让您找到了这个预期的解决方案,但没有可用的示例?不要使用正则表达式解析HTML。我一会儿就写一个答案。@Tek读了一遍笑话的答案。解析不匹配。用符合模因但过度重复的DOM答案来奖励不劳而获的问题不会教育新手。我相信教育新手包括告诉他们使用合适的工具来完成工作。如果您需要HTML文档中的某些内容,regex不是这里工作的工具。感谢您将其标记为重复。不要使用正则表达式解析HTML,他们应该使用DOMDoc。我不是那个投票否决您的人。然而,这可能就像我在另一个答案中评论的那样。您不能使用正则表达式从HTML检索信息
$pattern = "/<td class=\"nfo\">(.*?)<\/td>/s";
preg_match($pattern, $stringtomatch, $matches);
echo $matches[1];
16 GB, 1 GB RAM