Reputation: 63
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
Reputation: 9857
This should work if your pattern only changes by the id value:
$result = preg_replace('/::cck::([0-9]+)::\/cck::<br>::introtext::/', '', $text);
[0-9]+
matches 1 or more digits ranging from 0-9
\/
escapes the regex delimiter (or you can change the delimiter to something else)Update: A working example
Upvotes: 1