Solved

Replace HTML  tag attribute values using regex & php

Posted on 2011-03-09
3
875 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 500 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

729 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