替换PHP变量的内容(存储HTML标记)
我想替换PHP变量(替换PHP变量的内容(存储HTML标记),php,html,dom,Php,Html,Dom,我想替换PHP变量($html\u content)中的标签内容 变量$html\u content值如下所示: $html_content= "<table> <thead> <tr><th>Customer</th><th>Age</th><th>A</th></tr> </thead> <tbody> <tr><td>Tes
$html\u content
)中的标签内容
变量$html\u content
值如下所示:
$html_content= "<table>
<thead>
<tr><th>Customer</th><th>Age</th><th>A</th></tr>
</thead>
<tbody>
<tr><td>Test 1</td><td>25</td><td><a href='www.google.com'>Link</a></td></tr>
<tr><td>Test 2</td><td>30</td><td><a href='www.yahoo.com'>Link</a></td></tr>
<tr><td>Test 3</td><td>31</td><td><a href='www.rediff.com'>Link</a></td></tr>
...
...
...
</tbody>
</table>
使用此表,我在一页中显示了过滤结果,没有操作说明,而不是
标记。(此结果是名为reports.php
的页面的一部分)。
我只想将这些记录与操作
字段描述一起导出为PDF(我正在使用HTML转换为PDF类)。
在那个页面中,我将这些HTML值发送到那个页面,并将其存储在PHP变量($HTML\u content
)中的export\u pdf.PHP
页面中
表记录的大小不固定。。。它基于排序生成的结果
请任何人帮帮我。。。
提前感谢…尝试以下代码:
$html_content= preg_replace("/<a[^>]+\>(.*?)<\/a>/i", "Action will be taken soon.", $html_content);
输出
顾客
我们将很快采取行动。
我们将很快采取行动。
我们将很快采取行动。
您可以通过正则表达式执行此操作:
$rgReplace = ['foo', 'bar', 'baz'];
$i = 0;
$html_content=preg_replace_callback('/\<td[^\<\>]*\>\<a[^\<\>]*\>[^\<\>]*\<\/a\>\<\/td\>/',
function($rgMatches) use ($rgReplace, &$i)
{
return $rgReplace[$i++];
}, $html_content);
$rgReplace=['foo','bar','baz'];
$i=0;
$html\u content=preg\u replace\u回调('/\\API.谢谢Bora您的回复…但我需要结果来用不同的内容值替换每一行。内容取自表。我只想替换特定的客户操作。您能给我一个示例或粘贴到问题吗?我会更新Bora:我编辑了问题…请查看…您能理解吗?好的,您的替换文本ts将是
,昨天
等。文本根据什么变化?这些文本存储在哪里?在数组中?我必须知道。@Vasu更新为foreach
。您可以用foreach
替换查询结果,谢谢Alma do Mundo的回答。但我需要结果来用不同的nt内容值。内容取自表。只有我要替换的特定客户操作。我编辑了问题…请查看。。。
$html_content= preg_replace("/<a[^>]+\>(.*?)<\/a>/i", "Action will be taken soon.", $html_content);
foreach($row = mysql_fetch_assoc($result))
{
$html_content = preg_replace("/<a[^>]+\>(\w+)<\/a>/i", $row['action'], $html_content);
}
<table>
<thead>
<tr><th>Customer</th><th>Age</th><th>A</th></tr>
</thead>
<tbody>
<tr><td>Test 1</td><td>25</td><td>Action will be taken soon.</td></tr>
<tr><td>Test 2</td><td>30</td><td>Action will be taken soon.</td></tr>
<tr><td>Test 3</td><td>31</td><td>Action will be taken soon.</td></tr>
</tbody>
</table>
$rgReplace = ['foo', 'bar', 'baz'];
$i = 0;
$html_content=preg_replace_callback('/\<td[^\<\>]*\>\<a[^\<\>]*\>[^\<\>]*\<\/a\>\<\/td\>/',
function($rgMatches) use ($rgReplace, &$i)
{
return $rgReplace[$i++];
}, $html_content);