use regex to capture data between tags
(?:<\/?\w+)(?:\s+\w+(?:\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+)?)+\s*|\s*)\/?>([^<]*)?
then replace ' '
with' '
also catch before and after html:
^([^<>]*)<?
>([^<>]*)$
Edit: here you go ....
<?php
$data="dasdad asd a <table atrr=\"zxzx\"><tr><td>adfa a adfadfaf></td><td><br /> dfa dfa</td></tr></table> asdasd s ";
$exp="/((?:<\\/?\\w+)(?:\\s+\\w+(?:\\s*=\\s*(?:\\\".*?\\\"|'.*?'|[^'\\\">\\s]+)?)+\\s*|\\s*)\\/?>)([^<]*)?/";
$ex1="/^([^<>]*)(<?)/i";
$ex2="/(>)([^<>]*)$/i";
$data = preg_replace_callback($exp, create_function('$matches','return $matches[1].str_replace(" "," ",$matches[2]);'), $data);
$data = preg_replace_callback($ex1, create_function('$matches','return str_replace(" "," ",$matches[1]).$matches[2];'), $data);
$data = preg_replace_callback($ex2, create_function('$matches','return $matches[1].str_replace(" "," ",$matches[2]);'), $data);
echo $data;
?>
it works ... slightly modified, but it will work unchanged (but I don't think you understand the code;))
source
share