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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探