?
Solved

Replace HTML  tag attribute values using regex & php

Posted on 2011-03-09
3
Medium Priority
?
887 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.
Suggested Courses

764 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