preg_replace

问题描述 投票:0回答:1

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

<table width="567"

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

html regex preg-replace
1个回答
0
投票

尝试使用此模式:

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

替换$1将保留任何其他属性(请参阅(.*?)捕获组):

$text = preg_replace('/<table (.*?)width=["\']\s?\d+\s?(px|%)?\s*["\']/', '<table $1width="100%"', $text);
© www.soinside.com 2019 - 2024. All rights reserved.