0

Consider a php variable which contains a string of text, this text contains some html code, Say if I want to remove attributes from elements like <br> but not from <spam>

the string contained on the php variable would be something like this:

<br id="foo" style="display:none">
<span id="bar">sometext</span>
<br id="bun" />

will become this:

<br>
<spam id="bar">sometext</span>
<br />

Note: I would like to have a regex where I could just change the tag name manually something like:

<?php

$str='<br id="foo" style="display:none">
<spam id="bar">sometext</span>
<br id="bun" />';

$tagname = 'br'
$regex = "regexpar1".$tagname."regexpart2";

echo preg_replace($regex,'',$str);

Thanks a lot

EDIT: SOLUTION (thanks to @avinash-raj)

$str='<br id="foo" style="display:none">
<span id="bar">sometext</span>
<br id="bun" />';

$tagname = 'br';

echo preg_replace('~(<'.$tagname.')\b[^>]*?(?=\h*\/?>)~','\1',$str);
ccrez
  • 556
  • 4
  • 9
  • 1
    I don't see the link between HTML tag removal and regexes. I don't either see PHP or JScript in your post. – AFract Feb 18 '15 at 08:03
  • Just added all that thanks for the help, both with the issue and the noob question post – ccrez Feb 19 '15 at 07:34

3 Answers3

1

Use preg_replace function.

preg_replace('~(<br)\b[^>]*?(?=\h*\/?>)~', '\1', $str);

DEMO

\h matches any kind of horizontal white-space character.

Avinash Raj
  • 160,498
  • 22
  • 182
  • 229
1

Regular expression

You can try this:

(<)(br|div)([^>]+?)(\/?\s*>)

Regular expression visualization

As you can see in the above picture, you can adapt the Group 2 to your needs manually. On the picture, the regex is configured to find br or div tags.

DEMO

Usage

echo preg_replace( '/(<)(br|div)([^>]+?)(\/?\s*>)/', '\1\2\4', $str);

The / is the delimiter of the regular expression here. You can use any another character as long as it is a non-alphanumeric, non-backslash, non-whitespace character. This is why the / is preceded by a \ in the first regex of this post.

Here are examples with the regex delimited by other delimiters:

echo preg_replace( '%(<)(br|div)([^>]+?)(/?\s*>)%', '\1\2\4', $str);
echo preg_replace( '+(<)(br|div)([^>]+?)(/?\s*>)+', '\1\2\4', $str);
echo preg_replace( ',(<)(br|div)([^>]+?)(/?\s*>),', '\1\2\4', $str);

Avinash uses a ~ in his post.

Community
  • 1
  • 1
Stephan
  • 37,597
  • 55
  • 216
  • 310
  • Thanks so much for the speficic answer, is just awesome, it helps a lot to understand the concept, however on php I get a `Warning: preg_replace(): Unknown modifier '(' in D:\www\test.php on line 2`, when I try to do: `echo preg_replace('(]+?)(\/?\s*>)','',$str);` – ccrez Feb 19 '15 at 07:14
  • @ccrez Please, see the update in my post. – Stephan Feb 19 '15 at 08:49
  • Awesome it works, thanks a lot! – ccrez Feb 19 '15 at 17:43
0

just take id and style in a php variable and do anything with them what you want. here it is (remember make a unique id for each)

$id="somthing";
$style="style="display:none"";

just echo them at the place of id and style.

Ravi Sharma
  • 488
  • 2
  • 17