I'm new for PHP
I am trying to get topic number of link but not work.
echo $topicsave
is empty.
This my code.
$data = '
<a href="http://forums.inwing.com/index.php/topic,40500.0.html">test_curl</a>
';
preg_match_all('/\<a[^\?]+\/([^\"]+)\.\s*\>test_curl\<\/a\>/', $data, $match);
echo '<pre>',htmlspecialchars(print_r($match, true)),'</pre>';
if( count($match[0])){
foreach($match[1] as $vl){
preg_match_all('/topic\,([0-9]+\.[0-9]+)/', $vl, $m1);
if(count($m1[1]))
$topicsave = $m1[1][0];
echo $topicsave;
}
}
I want to get topic number 40500
please help me, topic is variable such as 120 or 2536
or 12456
.
Thank you.
To extract the topic number from link you can use following regex.
Regex: topic,(\\d+(\\.\\d+)*)\\.html
Explanation: What am doing is feeding your link to regex and extracting number between topic,
and .html
.
You can do it with this:
$re = "/topic,(?'topic'\\d+)/";
$str = "<a href=\"http://forums.inwing.com/index.php/topic,40500.0.html\">test_curl</a>";
preg_match($re, $str, $matches);
echo $matches['topic'];
Which will output:
40500
What I used here (?'topic'\\\\d+)
is a named group . It allows you to retrieve data from your matches with the name you used (here topic
).
If you need to do live tests, Regex 101 is great.
Try this solution:
$data = '<a href="http://forums.inwing.com/index.php/topic,40500.0.html">test_curl</a>';
preg_match_all('/topic,(.*?)\..*\.html/s', $data, $match);
echo $match[1][0]; // Output: 40500
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.