Re: strip_tags() Quandry....
| From: | Justin French | Date: | Wed, 28 May 2003 02:35:41 +0000 |
| Subject: | Re: strip_tags() Quandry.... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-149185@lists.php.net to get a copy of this message | ||
on 28/05/03 2:56 PM, CF High (noah@newminddesign.com) wrote:
> I've got a chunk of HTML text I'd like to format for insertion into our
> mySql db.
>
> Let's say $html_string equals the following:
>
> <tr>
> <td>1</td>
> <td>Bardo, Jesse</td>
> <td>S</td>
> <td>A</td>
> <td>Andover, MA</td>
> </tr>
>
> To setup this chunk of text for insertion I first use
> strip_tags($html_string); that results in:
>
> 1
> Bardo, Jesse
> S
> A
> Andover, MA
>
> I then use str_replace(",","",$html_string) to create a space delimited
> string.
exactly... for starters, there might be tabs (\t), newlines (\n, \r, \n\r),
plus multiple spaces (" ").
this is DEFINITELY a quick hack, adapted from code on
au.php.net/preg-replace... someone with more regular expression knowledge
can probably get this down to 1 or 2 lines, with a perfomance gain as well
:)
<?
$html = "<tr>
<td>1</td>
<td>Bardo, Jesse</td>
<td>S</td>
<td>A</td>
<td>Andover, MA</td>
</tr>";
$html = preg_replace ("'<[\/\!]*?[^<>]*?>'si", ' ',
$html);
$html = preg_replace ("'([\r\n])[\s]+'", ' ', $html);
$html = preg_replace ("([\s]+)", ' ', $html);
$html = trim($html);
echo "<pre>"; print_r($html); echo "</pre>";
?>
You'll need to test it thoroughly, and make sure it's ok on larger blocks of
text, like whatever your target source is, to make sure it doesn't bog
anything down.
Justin