dov6891 2013-10-14 19:02
浏览 11
已采纳

在html php中搜索和删除模式

I am trying to remove a piece of text from a string, but the text has a variable in the middle that makes it hard to make a match, how can I make a variable search and delete with PHP?

The text is "<div class="about_us_item"><h2>Privacy Policy</h2><div>::cck::34::/cck::<br>::introtext::<h3>Introduction</h3><p>John Blogs respects the privacy of each Visitor."

The part that needs to be stripped is "::cck::34::/cck::<br>::introtext::" but the "34" is the variable as it is an ID, so it could be "::cck::203::/cck::<br>::introtext::"

I have tried:

$html='"<div class="about_us_item"><h2>Privacy Policy</h2><div>::cck::34::/cck::<br>::introtext::<h3>Introduction</h3><p>John Blogs respects the privacy of each Visitor."';
$regex='/::cck::*::/cck::<br>::introtext::/';
preg_match ($regex,$html,$matches);
echo $matches;

Any help would be appreciated.

Thanks

  • 写回答

1条回答 默认 最新

  • dongshui9690 2013-10-14 19:11
    关注

    This should work if your pattern only changes by the id value:

    $result = preg_replace('/::cck::([0-9]+)::\/cck::<br>::introtext::/', '', $text);
    
    • The [0-9]+ matches 1 or more digits ranging from 0-9
    • The \/ escapes the regex delimiter (or you can change the delimiter to something else)

    Update: A working example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输