preg_replace <table 宽度=""

preg_replace <table width=""

不确定为什么下面的代码不起作用。有什么想法吗?

<table width="567"

$text = preg_replace('/<table width=["\']\s*\d+\s*(px|%)\s*["\']/', '<table width="100%"', $text);

试试这个模式:

/<table ([^>]*?)width=["\']\s?\d+\s?(px|%)?\s*["\']/

替换 </code> 将保留任何其他属性(参见 <code>(.*?) 捕获组):

$text = preg_replace('/<table (.*?)width=["\']\s?\d+\s?(px|%)?\s*["\']/', '<table width="100%"', $text);