Reputation: 115
I have this xml:
<RandomAlphaCharsA attr1="value1" attr2="value2 attrX="valueX" />
<RandomAlphaCharsB attr1="value1" attr2="value2 attrX="valueX" />
I need to strip the following:
<RandomAlphaCharsX
and />
, preserving only the attributes/values.
I tried this pattern:
$str = '<RandomAlphaChars attr1="value1" attr2="value2 attrX="valueX" />';
$str = preg_replace( "#<\w+[\s]{1}#", "", $str, -1 );`
var_dump( $str );
//it echoes string(46) "attr1="value1" attr2="value2 attrX="valueX" />"
This pattern in the other hand matches the closing tag:
preg_replace("#/>#", "", $str, -1);
How can I put them together in a single regexp?
Obviously, this is not working preg_replace("#<\w+[\s]{1}/>#", "", $str, -1);
I need to somehow exclude all kind of characters between <RandomAlphaChars
and />
and I need some help with that.
Edit: Wow you are fast. All answers work great!
Upvotes: 0
Views: 50
Reputation: 95
Here's mine:
$str = preg_replace('#<\w+\s+(.*?)\s*/>#s', '$1', $str);
Upvotes: 1
Reputation: 67968
<\S+|\/>
This should match both.See demo.
http://regex101.com/r/nW4yD9/1
Upvotes: 2
Reputation: 91385
Join them with the or operator |
:
$str = preg_replace( "#<\w+\s+|\s+/>#", "", $str);`
Upvotes: 1