如何仅从字符串的特定 stretch[href 标记] 之间的值中回显字符串?
how to do echo from a string, only from values that are between a specific stretch[href tag] of the string?
[PHP]我有一个用于存储字符串的变量(一个 BIIGGG 页面源代码作为字符串),我只想回显有趣的字符串(我需要提取以在项目中使用,几十个它们), 它们在标签 <a href="HERE"></a>
的引号内
但我只想捕获以字母开头的值:N (news)
[<a href="/news7044449/exclusive_news_sunday_"]
<a href="/n[ews7044449/exclusive_news_sunday_]"
that is, I think you will have to work with match using: [a href="/n]
如何定义回显将删除变量的所有文本,仅显示:
请注意,还有其他 hrefs 标签的值以其他字母开头,例如字母 'P':href="/profiles...(这我不感兴趣。)
$string = '</div><span class="news-hd-mark">HD</span></div><p><a href="/news7044449/exclusive_news_sunday_" title="exclusive_news_sunday_">exclusive_news_sunday_</a></p><p class="metadata"><span class="bg"><a href="/profiles/czechav">Czech AV</a><span class="mobile-hide"> - 5.4M Views</span>
- <span class="duration">7 min</span></span></p></div><script>xv.thumbs.preparenews(7044449);</script>
<div id="news_31720715" class="thumb-block "><div class="thumb-inside"><div class="thumb"><a href="/news31720715/my_sister_running_every_single_morning"><img src="https://static-hw.xnewss.com/img/lightbox/lightbox-blank.gif"';
我想象的是这样的:
$removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n = ('/something regex expresion I think /' or preg_match, substring?);
echo $string = str_replace($removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n,'',$string);
预期输出: /news7044449/exclusive_news_sunday_
注意:不一定要通过变量,它可以是从 .txt 文件中提取提取物的地方,不一定是变量。
谢谢。
我相信这会对她有所帮助。
<?php
$source = file_get_contents("code.html");
preg_match_all("/<a href=\"(\/n(?:.+?))\"[^>]*>/", $source, $results);
var_export( end($results) );
循序渐进的正则表达式:
从 Valdeir 的回答中获取 $results 数组中的链接:
foreach ($results as $r) {
echo $r;
// alt: to display them with an HTML break tag after each one
echo $r."<br>\n";
}
[PHP]我有一个用于存储字符串的变量(一个 BIIGGG 页面源代码作为字符串),我只想回显有趣的字符串(我需要提取以在项目中使用,几十个它们), 它们在标签 <a href="HERE"></a>
但我只想捕获以字母开头的值:N (news)
[<a href="/news7044449/exclusive_news_sunday_"]
<a href="/n[ews7044449/exclusive_news_sunday_]"
that is, I think you will have to work with match using: [a href="/n]
如何定义回显将删除变量的所有文本,仅显示:
请注意,还有其他 hrefs 标签的值以其他字母开头,例如字母 'P':href="/profiles...(这我不感兴趣。)
$string = '</div><span class="news-hd-mark">HD</span></div><p><a href="/news7044449/exclusive_news_sunday_" title="exclusive_news_sunday_">exclusive_news_sunday_</a></p><p class="metadata"><span class="bg"><a href="/profiles/czechav">Czech AV</a><span class="mobile-hide"> - 5.4M Views</span>
- <span class="duration">7 min</span></span></p></div><script>xv.thumbs.preparenews(7044449);</script>
<div id="news_31720715" class="thumb-block "><div class="thumb-inside"><div class="thumb"><a href="/news31720715/my_sister_running_every_single_morning"><img src="https://static-hw.xnewss.com/img/lightbox/lightbox-blank.gif"';
我想象的是这样的:
$removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n = ('/something regex expresion I think /' or preg_match, substring?);
echo $string = str_replace($removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n,'',$string);
预期输出: /news7044449/exclusive_news_sunday_
注意:不一定要通过变量,它可以是从 .txt 文件中提取提取物的地方,不一定是变量。
谢谢。
我相信这会对她有所帮助。
<?php
$source = file_get_contents("code.html");
preg_match_all("/<a href=\"(\/n(?:.+?))\"[^>]*>/", $source, $results);
var_export( end($results) );
循序渐进的正则表达式:
从 Valdeir 的回答中获取 $results 数组中的链接:
foreach ($results as $r) {
echo $r;
// alt: to display them with an HTML break tag after each one
echo $r."<br>\n";
}