php - 从不带 URL 参数的 HTML src 标签中获取 URL
问题描述
我在一个名为例如的变量中有这个内容$iframe
$iframe='<iframe title="Example Iframe" width="640" height="360" src="https://example.com/mypage?parameter=1" frameborder="0" allowfullscreen></iframe>';
而且我需要剥离除 src URL 之外的所有内容,不带参数
在上面的示例中,最终结果应该是这样的:
echo $iframe;
https://example.com/mypage
我不确定是否有一个函数可以在 HTML 字符串中搜索属性,然后可能使用一些正则表达式来去除参数......
解决方案
尝试正则表达式
/src=\"(.*?)(\?.*\"|\")/
<?php
$re = '/src=\"(.*?)(\?.*\"|\")/';
$str = '\'<iframe title="Example Iframe" width="640" height="360" src="https://example.com/mypage?parameter=1" frameborder="0" allowfullscreen></iframe>\'';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches[0][1]);
推荐阅读
- c# - Json.net DeserializeAnonymousType 返回 BigInteger 而不是 UInt64
- java-audio - 有没有办法将 JavaFX AudioClip 声音的多线程混合结果记录到磁盘?
- javascript - 使用 jQuery 从下拉菜单中删除项目不起作用
- node.js - Docker 容器正在运行但未列出且无法停止
- javascript - 通过 React Bootstrap DropdownButton 和从数组呈现的下拉项更新本地状态
- c# - 了解控制台应用程序中的 .net Core 依赖注入
- django - 当我尝试使用 requirements.txt 文件#Django 安装库时出现此错误
- macos - macOS:当用户移动父文件夹时如何处理文件引用
- c# - 如何从图像中获取 HSV 值
- google-bigquery - BigQuery:Google Ads 报告转换:地理绩效报告:GeoStats 表为空