Skip to content

Instantly share code, notes, and snippets.

@cornellsteven
Last active August 29, 2015 14:16
Show Gist options
  • Save cornellsteven/e3671ceb572fc4919a15 to your computer and use it in GitHub Desktop.
Save cornellsteven/e3671ceb572fc4919a15 to your computer and use it in GitHub Desktop.
Quick (and probably imperfect) regex for removing all attributes form HTML
<?php
$string = '<table border="2" cellpadding="5" cellspacing="2">
<tr>
<th style="width: 50%;" align="center">Header 1</th>
<th align="center">Header 2</th>
</tr>
<tr>
<td class="odd">Content 1</td>
<td class="even" style="text-align: left;">Content 2</td>
</tr>
<tr>
<td class="odd">Content 3</td>
<td class="even">Content 4</td>
</tr>
</table>';
echo preg_replace('/<([a-zA-Z]+)[^>]*>/', '<$1>', $string);
/*
Would output:
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Content 1</td>
<td>Content 2</td>
</tr>
<tr>
<td>Content 3</td>
<td>Content 4</td>
</tr>
</table>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment