Solved

Replace HTML  tag attribute values using regex & php

Posted on 2011-03-09
3
857 Views
Last Modified: 2013-12-13
Hello,
I have a php function that spits out some HTML tags.

Could you help me write a regex that replaces all values of the width attribute by 300, and stores the old width in a variable.

Many thanks


<?php
$oldwidth="";
$string   = '<object width="400" height="325"><param name="movie" value="http://www.youtube.com/v/b9JAHhqV6Ko&amprel=0&ampegm=0&ampshowinfo=0&ampfs=1"></param><param name="wmode" value="transparent"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/b9JAHhqV6Ko&amprel=0&ampegm=0&ampshowinfo=0&ampfs=1" type="application/x-shockwave-flash" width="400" height="325" allowFullScreen="true" wmode="transparent"></embed></object>';
?>

Open in new window

0
Comment
Question by:betwin
  • 2
3 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Perhaps you're looking for something like this?
$string   = '<object width="400" height="325"><param name="movie" value="http://www.youtube.com/v/b9JAHhqV6Ko&amprel=0&ampegm=0&ampshowinfo=0&ampfs=1"></param><param name="wmode" value="transparent"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/b9JAHhqV6Ko&amprel=0&ampegm=0&ampshowinfo=0&ampfs=1" type="application/x-shockwave-flash" width="400" height="325" allowFullScreen="true" wmode="transparent"></embed></object>';
preg_match('/(?<=width=")(\d+)(?=")/', $string, $oldwidths);
$string = preg_replace('/(?<=width=")\d+(?=")/', '300', $string);

var_dump($oldwidths);
var_dump($string);

Open in new window

0
 

Author Comment

by:betwin
Comment Utility
Thank you very much, it works a treat :)
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
NP. Glad to help  : )
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now