[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Replace HTML  tag attribute values using regex & php

Posted on 2011-03-09
3
Medium Priority
?
902 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 35086402
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
ID: 35094425
Thank you very much, it works a treat :)
0
 
LVL 75

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

656 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