user2597255
user2597255

Reputation: 63

Search and remove pattern in 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

Upvotes: 0

Views: 264

Answers (1)

AlexP
AlexP

Reputation: 9857

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

Upvotes: 1

Related Questions