php - Replace string dynamically -
how can remove string:
class="size-full wp-image-1561 "
from string:
class="size-full wp-image-1561 " alt="class-a warehouse facility developed panattoni europe in germany rudolph logistik gruppe." src="http://europe-re.com/wp-content/uploads/2012/12/class-a-warehouse-facility-developed-by-panattoni-europe-in-germany-for-rudolph-logistik-gruppe.jpg"
consider class changes every record. how can dynamically? "remove class="(whatever inside here)"
full string.
thank in advance!
if regex means have @ hand, can match
\bclass="[^"]*"\s*
the, replace empty string. \b
makes sure matching class
, not subclass
.
with [^"]*
, can match 0 or more characters other "
.
with \s*
, can trim string automatically.
see demo
however, if deal php, you'd better using domdocument. similar to
$html = <<<html <div id="res">some text inside div <img class="size-full wp-image-1561 " alt="class-a warehouse facility developed panattoni europe in germany rudolph logistik gruppe." src="http://europe-re.com/wp-content/uploads/2012/12/gruppe.jpg"> <img alt="class-a warehouse facility developed panattoni europe in germany rudolph logistik gruppe." src="http://europe-re.com/wp-content/uploads/2012/12/gruppe.jpg"> </div> html; $dom = new domdocument(); @$dom->loadhtml($html); $dom->preservewhitespace = false; $images = $dom->getelementsbytagname('img'); $imgs = array(); foreach($images $img) { if ($img->attributes->getnameditem("class") != null) { $imgs[] = $img; } } foreach($imgs $img) { $img->parentnode->removechild($img); } $str = $dom->savehtml(); echo $str;
output of sample ideone demo:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" "http://www.w3.org/tr/rec-html40/loose.dtd"> <html><body><div id="res">some text inside div <img alt="class-a warehouse facility developed panattoni europe in germany rudolph logistik gruppe." src="http://europe-re.com/wp-content/uploads/2012/12/gruppe.jpg"></div></body></html>
Comments
Post a Comment