duanjia4097 2012-04-27 06:55
浏览 47
已采纳

如何在第一个(变量)html-tag之间提取部分字符串?

I have some strings:

$string1 = '<p><strong>Extract me</strong></p><p>Leave me</p>';
$string2 = '<strong>Extract me</strong>Leave me';
$string3 = '<span style="font-weight: bold">Extract me</span><br /><span>Leave me</span>';

Let's check $string3:

The first tag of the string is <span> So the text between the first <span> and the first </span> wants to be extracted.

Extracted shall mean: remove it from $stringX and save it into $extractedX

How would I do this?

  • 写回答

4条回答 默认 最新

  • duanqiongchong0354 2012-04-27 07:02
    关注
    [^>]*?(?=<\/.*>)
    

    What you should do is use an assertion. [^>]*? searches for any character that is NOT a >. This should be fine since if you need to use > as text, it would need to be escaped as &gt;. Then it searches for the first closing tag as denoted by <\/.*>. The (?=) around it tells the regex engine not to include it in the match.

    http://regexr.com?30pkm

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?