[英]php Regular expressions basic
I want to regex thread_id in this html code by using php 我想使用php在此html代码中正则表达式thread_id
<a id="change_view" class="f_right" href="pm.php?view=chat&thread_id=462075438105382912">
I wrote this code however it return empty array to me 我写了这段代码,但是它向我返回了空数组
$success = preg_match_all('/pm\.php\?view=chat&thread_id=([^"]+)/', $con, $match2);
is there any problem in my php code ? 我的php代码有什么问题吗?
Well, you said it is giving you an empty array. 好吧,您说过它给您一个空数组。 But it is not. 但事实并非如此。 Here is the value returned by print_r()
这是print_r()
返回的值
Array
(
[0] => Array
(
[0] => pm.php?view=chat&thread_id=462075438105382912
)
[1] => Array
(
[0] => 462075438105382912
)
)
But It is not returning what you want it to. 但是它并没有返回您想要的东西。 The regular expression to get string that comes after thread_id=
and before &
or "
is : 要获取在thread_id=
和&
或"
之前的字符串的正则表达式为:
/(?<=thread_id=).*(?=\"|&)/
Working example : 工作示例:
<?php
$con = '<a id="change_view" class="f_right" href="pm.php?view=chat&thread_id=462075438105382912">link</a>';
$match2 = Array();
preg_match_all('/(?<=thread_id=).*(?=\"|&)/', $val, $arr);
echo "<pre>";
print_r($arr);
echo "</pre>";
?>
Output : 输出:
Array
(
[0] => Array
(
[0] => 462075438105382912
)
)
if (preg_match('/thread_id=[0-9]*/', $line, $matches))
$thread_id = $matches[0];
如果您只在寻找thread_id,则应该这样做。
$success = preg_match_all('/(thread_id=)([\d]+)/', $con, $match2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.